September 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

My First Microsoft Sync Framework Application

  • April 6, 2009
  • By Matt Goebel, Rachel Baker
  • Send Email »
  • More Articles »

The Microsoft Sync Framework and Visual Studio 2008 provide Windows desktop developers with a powerful tool for creating occasionally-connected applications that can synchronize data with a central repository when connected. In the following example, we will use the Microsoft Sync for ADO.NET portion of the framework to create a local data store with a SQL Compact Edition database that syncs with the AdventureWorks database in SQL Server 2005. We will manage salesperson information from our application while offline and sync this information back to the central AdventureWorks database.

In order to follow along with the sample shown in this article, you will need Visual Studio 2008 with SP1, the Microsoft Sync Framework (MSF), and SQL Server 2005 with the AdventureWorks sample database. Visual support for MSF was introduced in VS 2008 (prior to SP1); however, the "Configure Tables for Offline Use" dialog we will discuss later only shows the tables included in the user's default schema (e.g., dbo). The AdventureWorks database uses several schemas, and the tables we use in this example are outside of the dbo schema and will not appear in the VS 2008 dialog. VS 2008 SP1 shows tables from all schemas in the dialog.

To begin, open Visual Studio 2008 and create a new Windows Forms project called "My MSF App." Next, we will add the local database that will be used to store or cache the data offline. Right click on the project file 'My MSF App' in Solution Explorer, select Add, and then New Item. Select the template for a Local Database Cache. Change the name to "AWLocalDataCache.sync" (the "AW" stands for AdventureWorks).


Click here for larger image

Listing 1.1 Add a Local Database Cache

Click Add. Visual Studio will now launch the "Configure Data Synchronization" dialog. This is where we set up the server and local database connections. From the Server connection dropdown list, create a new connection to the AdventureWorks database. Once you have selected a server connection, Visual Studio will automatically populate the Client connection with a connection to a new SQL Compact database.


Click here for larger image

Listing 1.2 Create the Database Connections

Note that the client connection has an "sdf" extension (as opposed to an SQL Server database "mdf" file extension).

The next step is configuring which tables in the database to cache and make available offline. Click the Add button in the bottom left-hand corner of the dialog. The "Configure Tables for Offline Use" dialog is displayed. Here, we will choose what tables the application will be caching and syncing. For this example, we will be using just a few tables to pull up salesperson information and enable the ability to view, update, and add to that information offline. Using the Tables list, select the following tables to be included:

  • Employee
  • SalesPerson
  • SalesTerritory

We will leave the fields to the right on the default settings. These fields are used to control how the sync framework keeps track of inserts, updates, and deletes.


Click here for larger image

Listing 1.3 Select the tables to synchronize

Click OK to exit the dialog once you have selected all the tables listed above. Visual Studio will return us to the "Configure Data Synchronization" dialog and will now display the tables we selected in the Cached tables list.


Click here for larger image

Listing 1.4 Configuring synchronization cont'd

Note that if you are using SQL Server 2008, the checkbox below the Database Connections group box, "Use SQL Server change tracking," will be enabled. SQL Server change tracking is a new feature in SQL Server 2008 that will dramatically increase performance of syncing. There is unavoidable performance overhead to tracking changes and keeping the data in sync across multiple data sources. SQL Server change tracking reduces this performance overhead to the equivalent of maintaining a second index on your table. This marks a huge improvement over triggers, additional columns of data, and tombstone tables that are traditionally required in a sync environment using SQL Server 2005.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel