Share via


How to: Initiate Synchronization Between Local and Remote Databases

After you add a Local Database Cache and provide configuration information in the Configure Data Synchronization dialog box, you must still add code to your application to actually synchronize the local and remote databases. Initiate synchronization by calling the Synchronize method of a SyncAgent.

It is important to understand that synchronization only synchronizes the data between the remote data and the local database (.sdf). The data in your application still has to be reloaded from the database. Remember to reload your application data source with the updated data from the local database. For example, call the TableAdapter.Fill method to load your dataset's data table with the updated data from the local database.

The Configure Data Synchronization dialog box includes a Show Code Example link that provides a code example with the correct syntax for calling the Synchronize method. Include this code example anywhere in the application where synchronization has to be performed.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Procedure

To initiate synchronization between the local and remote databases

  1. Double-click the .sync file in Solution Explorer to open the Configure Data Synchronization dialog box.

  2. Click the Show Code Example link in the Configure Data Synchronization dialog box.

  3. Click Copy code to the clipboard and then click Close.

  4. Paste the code example that you copied in the previous step into your application. Paste it where you want to initiate the synchronization call. The following code shows how to initiate synchronization for an application configured to synchronize the Northwind customers table:

    ' Call the Synchronize method to synchronize
    ' data between local and remote databases.
    Dim syncAgent As NorthwindCacheSyncAgent = New NorthwindCacheSyncAgent()
    Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics =
        syncAgent.Synchronize()
    
    ' After synchronizing the data, refill the
    ' table in the dataset.
    Me.CustomersTableAdapter.Fill(NorthwindDataSet.Customers)
    
    // Call the Synchronize method to synchronize
    // data between local and remote databases.
    NorthwindCacheSyncAgent syncAgent = new NorthwindCacheSyncAgent();
    Microsoft.Synchronization.Data.SyncStatistics syncStats;
    syncStats = syncAgent.Synchronize();
    // After synchronizing the data, refill the
    // table in the dataset.
    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
    
  5. After synchronizing, add code to repopulate the data source in your application with the data in the synchronized local database.

See Also

Tasks

Walkthrough: Creating an Occasionally Connected Application

How to: Configure Data Synchronization in an Application

Concepts

Occasionally Connected Applications Overview

SQL Server Compact 3.5 and Visual Studio