February 26, 2021
Hot Topics:

Getting Started with Microsoft Synchronization Services for ADO.NET

  • By Rachel Baker
  • Send Email »
  • More Articles »

Scroll down or search for the SyncSession class definition. The signature looks like this:

public partial class SyncSession

This is where the redundant definitions begin. Comment out or delete all the code starting from here all the way down to and including the SyncServerInfo class definition. Save the references file. That is all there is to it. If you want to see exactly what to comment out, take a look at the References.cs file in the sample code provided.

Now, you can go back to finish up your SyncAgent constructor. The sync services framework provides a ServerSyncProviderProxy wrapper class; it accepts a web service with the methods you have defined in your ServerSyncProvider web service. The code looks like this:

Listing 13: Remote provider hook-up code

   serverSyncWebService = new
   SyncServicesSampleService.SyncServicesSampleService ();
this.RemoteProvider = new

Defining the SyncTables is pretty clear-cut. You will define the table, set its creation option, describe the sync direction, and add the sync table to the SyncAgent. You can see the differences among the tables you created by inspecting the code below:

Listing 14: Sync tables code

#region User Table

SyncTable userSyncTable = new SyncTable("User");
userSyncTable.CreationOption =
userSyncTable.SyncDirection = SyncDirection.UploadOnly;



#region Item Table

SyncTable itemSyncTable = new SyncTable("Item");
itemSyncTable.CreationOption =
itemSyncTable.SyncDirection = SyncDirection.DownloadOnly;



#region Customer Table

SyncTable customerSyncTable = new SyncTable("Customer");
customerSyncTable.CreationOption =
customerSyncTable.SyncDirection = SyncDirection.Bidirectional;



#region CustomerFavoriteItem Table
SyncTable customerfavoriteitemSyncTable = new
customerfavoriteitemSyncTable.CreationOption =
customerfavoriteitemSyncTable.SyncDirection =



The final piece of the SyncAgent constructor is to add the @UserID custom parameter that you use to determine which rows to update from the server. This is the method you can use to add any custom parameters you will need to the sync layer. The syntax is as follows:

this.Configuration.SyncParameters.Add("@UserID", Program.ClientID);

Still with me? You are almost there. Now, it is time to create the database and add the synchronization logic. Back in Program.cs, you will add a check to see whether the client database file exists. If it does not, you will create the file and call the synchronization method to pull down the tables and data from the server database. You'll want to add some data to the tables if you want to see anything brought down at this point. Otherwise, you can add it later when you're playing with the synchronization to see all the information get shuffled around. Add this method to your Program class and call it from Main before the form is created:

Listing 15: Create client database code

public static void CreateClientDatabase()
    string clientConn = ConfigurationManager

   using (SqlCeConnection sqlconn = new SqlCeConnection(clientConn))
      if (!File.Exists(sqlconn.Database))
         SqlCeEngine sqlCeEngine = new SqlCeEngine(clientConn);

         //if first time creating, run synchronization
         //add this client's UserID to the User table

Notice that I have an AddClientID method that will insert this client's UserID into the client database so that any customers added by this user will have a UserID to map back to. Depending on how you implemented the generation of the clientID, code up this method accordingly. Remember that your User table is an upload-only table, so the next time the client syncs with the server, the new UserID will get pushed up to the server database.

Page 7 of 8

This article was originally published on December 17, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date