Published Monday, May 21, 2007 6:35 AM by mtaulty

First Steps with ADO.NET Synchronisation Services

I've had a first attempt at playing with the new ADO.NET synchronisation services this weekend so I thought I'd share here.

ADO.NET Synchronisation Services is a technology for helping with those online/offline applications where you need to get data down to be stored on a laptop to enable mobile working (device support isn't something that it's in the first version AFAIK).

Here's some links;

Download the bits ("Orcas" beta 1)

Download the docs ("Orcas" beta 1)

Subscribe to the "Synchronizer" blog

To take my simplistic view of Synchronisation Services, it's about having a local SQL Server Compact Edition database which your application talks to and the Sync Services helps with getting the data in that database sync'd with a remote DB to which connectivity isn't always available.

I say helps because Sync Services is definitely more of a framework that a developer can use to build this kind of sync. support into their app rather than a product which does it for them.

There's no shortage of ways to do this already - SQL Server supports technologies such as Merge Replication which achieves a similar thing but there are differences in Sync Services (quite a lot, but here's a few);

  1. Sync Services supports multiple back ends, not just SQL Server (anything that's OLEDB compliant, AFAIK)
  2. Sync Services doesn't necessarily have to talk to a DB as a back-end at all, you can use it over N-Tier and have it talk to your own service rather than a DB.
  3. Sync Services is driven by programmability rather than configuration.
  4. Sync Services supports views down on the client.

It seems to me that there's quite a lot of moving parts to Sync Services and so kicking off coding against it seemed a little daunting but, actually, once you get going it's not particularly tough. I copied this picture out of the books online update (hyperlink above);

So, we have;

  1. The client and server databases that we want to sync.
  2. The client sync provider (this abstracts the sync agent from the details of the client DB). There's only a SQL CE one of these provided AFAIK right now.
  3. A set of SyncTables that contain the details about what we want to synchronise between client and server. These can be grouped to ensure that a bunch of stuff is all sync'd at once (i.e. within the bounds of a transaction).
  4. A SyncAgent that does the synchronisation for us.
  5. A set of SyncAdapters. These essentially control the how in terms of how we take data in one place and get it into the other place.
  6. A server sync provider (this abstracts the sync agent from the details of the server DB). There's a generic "DB" version of this right now which speaks OLEDB.

So, let's say that I've got this little table sitting in my SQL Server;

create table fruitSales
(
  id int identity not null primary key,
  fruit nchar(20) not null,
  quantity int not null,
  price money not null
)

and I want to synchronise that down to my client. I have a lot of choices about what kind of synchronisation I want to perform (e.g. snapshot from server to client, download incremental from server to client, upload incremental from client to server, bidirectional) and the simplest one is to do a snapshot.

The following code has references to Microsoft.Synchronization.Data, Microsoft.Synchronization.Data.Client, Microsoft.Synchronization.Data.Server.

So, I'll need a client provider for my local CE database;

 

      SqlCeClientSyncProvider clientProvider =
        new SqlCeClientSyncProvider(@"datasource=c:\temp\syncdb.sdf", true);
 

And I'll be needing a server provider for my remote SQL database;

 

     SqlConnection sourceDbCon = new SqlConnection(
       @"server=.;database=fruitData;integrated security=sspi");

      DbServerSyncProvider serverProvider = new DbServerSyncProvider()
      {
        Connection = sourceDbCon
      };
      

And I need a SyncTable specifying that I'm wanting to synchronise my fruitSales table;

 

      SyncTable syncTable = new SyncTable("fruitSales")
      {
        CreationOption = TableCreationOption.DropExistingOrCreateNewTable,
        SyncDirection = SyncDirection.Snapshot 
      };

Note that I'm asking to drop any existing table and that my direction is a snapshot synchronise from server to client.

Then I need a SyncAdapter to actually do the work on that table for me;

 

 SyncAdapter adapter = new SyncAdapter("fruitSales");

      SqlCommand selectCommand = new SqlCommand("select * from fruitSales", 
        sourceDbCon);

      selectCommand.CommandType = CommandType.Text;

      adapter.SelectIncrementalInsertsCommand = selectCommand;

 

Notice that we tell the SyncAdapter how to get data (the SelectIncrementalInsertsCommand - bit weird naming for me at first but I got it in the end) by giving it a SqlCommand but it's really an IDbCommand so any provider you've got will work there.

Now, we need to let our server provider know about our adapter;

 
serverProvider.SyncAdapters.Add(adapter);
 

and we finally just need a SyncAgent to do the work for us and we need to tell it about the client provider, the server provider and the tables that we want do sync;

 
    SyncAgent agent = new SyncAgent() 
      {
        ClientSyncProvider = clientProvider,
        ServerSyncProvider = serverProvider                          
      };

      agent.SyncTables.Add(syncTable);

 

With all that in place, we can do the synchronisation (there's an async version with events and so on rather than just this synchronous version);

 

agent.Synchronize();
 

and we have synchronisation :-) I've managed to move a single table's worth of data down from the server to my client.

I'll follow up with another post about how I took this slightly further to do incremental downloads of data from the server to the client.

# Second Steps with ADO.NET Synchronisation Services @ Monday, May 21, 2007 6:56 AM

Following up on this post where I just got started with Sync Services, if I want to do more than just...

Mike Taulty's Blog

# First Steps with ADO.NET Synchronisation Services @ Tuesday, May 22, 2007 2:01 PM

ADO.NET Synchronisation Services is a technology for helping with those online/offline applications where

help.net

# Link Listing - May 24, 2007 @ Thursday, May 24, 2007 8:00 PM

WCF Security References [Via: nluria ]  Smart Client Software Factory v2 is now available [Via: Public...

Christopher Steen

# Link Listing - May 24, 2007 @ Thursday, May 24, 2007 8:02 PM

WCF Security References [Via: nluria ] Smart Client Software Factory v2 is now available [Via: Public...

Christopher Steen

# Wordpress Tips » Wordpress Plugins @ Saturday, May 26, 2007 12:33 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugins-8/

Wordpress Tips » Wordpress Plugins

# Wordpress Tips » Wordpress Plugin @ Saturday, May 26, 2007 1:22 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-11/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Saturday, May 26, 2007 4:12 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-2/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Saturday, May 26, 2007 5:59 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-5/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Saturday, May 26, 2007 1:41 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-10/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Saturday, May 26, 2007 8:57 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-4/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Wordpress Plugin @ Saturday, May 26, 2007 9:14 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-58/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Saturday, May 26, 2007 10:36 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-18/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Wordpress Plugins @ Saturday, May 26, 2007 10:38 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugins-64/

Wordpress Tips » Wordpress Plugins

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 12:07 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-5/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 1:15 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-19/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 1:39 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-6/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Wordpress Plugin @ Sunday, May 27, 2007 1:39 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-65/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 2:14 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-20/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Wordpress Plugins @ Sunday, May 27, 2007 2:25 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugins-68/

Wordpress Tips » Wordpress Plugins

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 2:39 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-22/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 3:20 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-9/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 3:38 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-10/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Wordpress Plugin @ Sunday, May 27, 2007 7:17 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-66/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 7:18 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-24/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 7:31 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-13/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Wordpress Plugin @ Sunday, May 27, 2007 7:31 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-67/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 7:40 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-26/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 7:51 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-27/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 11:06 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-28/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Wordpress Plugins @ Sunday, May 27, 2007 11:10 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugins-71/

Wordpress Tips » Wordpress Plugins

# Wordpress Tips » Wordpress Plugin @ Sunday, May 27, 2007 11:22 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-68/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 11:33 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-29/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 11:54 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-31/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 3:03 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-14/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Sunday, May 27, 2007 7:06 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-15/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Wordpress Plugin @ Sunday, May 27, 2007 7:46 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-69/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 7:48 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-32/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Wordpress Plugins @ Sunday, May 27, 2007 11:06 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugins-74/

Wordpress Tips » Wordpress Plugins

# Wordpress Tips » Link Listing - May 24, 2007 @ Sunday, May 27, 2007 11:13 PM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-33/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Link Listing - May 24, 2007 @ Monday, May 28, 2007 3:26 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-34/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Wordpress Plugin @ Monday, May 28, 2007 3:49 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/wordpress-plugin-70/

Wordpress Tips » Wordpress Plugin

# Wordpress Tips » Top Wordpress Plugins for the Smart Blogger @ Monday, May 28, 2007 3:55 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/top-wordpress-plugins-for-the-smart-blogger-2/

Wordpress Tips » Top Wordpress Plugins for the Smart Blogger

# Wordpress Tips » Link Listing - May 24, 2007 @ Monday, May 28, 2007 8:08 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-35/

Wordpress Tips » Link Listing - May 24, 2007

# Wordpress Tips » Top Wordpress Plugins for the Smart Blogger @ Monday, May 28, 2007 8:47 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/top-wordpress-plugins-for-the-smart-blogger-3/

Wordpress Tips » Top Wordpress Plugins for the Smart Blogger

# Wordpress Tips » Top Wordpress Plugins for the Smart Blogger @ Monday, May 28, 2007 8:58 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/top-wordpress-plugins-for-the-smart-blogger-4/

Wordpress Tips » Top Wordpress Plugins for the Smart Blogger

# Wordpress Tips » Quick custom text ad placement in WordPress blog categories @ Monday, May 28, 2007 9:01 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/quick-custom-text-ad-placement-in-wordpress-blog-categories-16/

Wordpress Tips » Quick custom text ad placement in WordPress blog categories

# Wordpress Tips » Top Wordpress Plugins for the Smart Blogger @ Wednesday, May 30, 2007 8:16 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/top-wordpress-plugins-for-the-smart-blogger-5/

Wordpress Tips » Top Wordpress Plugins for the Smart Blogger

# Wordpress Tips » Link Listing - May 24, 2007 @ Wednesday, May 30, 2007 9:09 AM

PingBack from http://wordpress-ads.com/wordpress-plugin/link-listing-may-24-2007-36/

Wordpress Tips » Link Listing - May 24, 2007

# Womens Discount Perfume » Not vera wang princess perfume but still interesting… @ Tuesday, October 30, 2007 2:39 PM

PingBack from http://discount-perfume-hq.com/?p=1785

Womens Discount Perfume » Not vera wang princess perfume but still interesting…

# Womens Discount Perfume » Not miniature perfume but still interesting… @ Wednesday, October 31, 2007 6:51 AM

PingBack from http://discount-perfume-hq.com/?p=2685

Womens Discount Perfume » Not miniature perfume but still interesting…