Tuesday 9 June 2009

Here is how to create a synchronization with a database and cached data1

Here is a quick example of how to used the synchronization framework and cached data on the client:

1. Create a Windows Forms application
2. Under the tool bar select Data/Add New Data Server
3. Select database
4. Click the new connection button and select the database to connect to
5. Don't save the connection as
6. Select the tables to synchronize Select Enable local database caching (This creates a mini local database)
7. Select Synchronization Mode Incremental (Snap shot means every time the database is synchronized all data is downloaded ) Select Use SQL Server change tracking
8. Select Update server for incremntal changes
Save SQL scripts in the project for later use (This is useful if you move the database)
After this you will get a file like TEST_AynchCache.syncIf you double click on this you get a hyperlink to "Show Code Example".
This code can be used to perform the synchronization.
Here is an example of how to populate and synchronize a data grid view

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TST_SLAVETableAdapter.Fill(Me.TEST_SYNCHDataSet.TST_SLAVE)
Me.TST_MASTERTableAdapter.Fill(Me.TEST_SYNCHDataSet.TST_MASTER)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim syncAgent As TEST_SYNCHCacheSyncAgent = New TEST_SYNCHCacheSyncAgent()
Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize()
Me.TST_SLAVETableAdapter.Fill(Me.TEST_SYNCHDataSet.TST_SLAVE)
End Sub

syncStats.TotalChangesDownloaded contains the number of changes that where made