Using Sync Services with Visual Studio 2008
Prior to Visual Studio 2008, using Sync Services involved writing large amounts of code and lots of database preparation. Fortunately, Visual Studio 2008 ships with the Sync Designer for Sync Services, which lets you use Sync services without writing much code.
First, create a Windows Forms application using Visual Studio 2008 and name the project SyncServices.
Add a new item to the project by right-clicking on the project name in Solution Explorer and then selecting Add>New Item…. Select the Local Database Cache template and name it LocalAuthors.sync.
After you add the item to the project, the Configure Data Synchronization wizard will appear.
First, select the connection to the SQL Server database that you want to use. For this article, you'll use the pubs sample database installed on the default instance of the SQL Server 2005 Express database (assuming you already have the database). Once you've selected the database, the pubs.sdf database is automatically selected for you. The pubs.sdf database is the SQL Compact 3.5 database that you will use locally on the client. It's a cached copy of the pubs table on the database server.
Next, click the Add button to select the tables you want to use in your application. The Configure Tables for Offline Use dialog will now appear (see Figure 2).
Figure 2. Configure Tables for Offline Use: This dialog allows you to select the tables you want to use in your application.
Figure 3. The Configure Data Synchronization Wizard: Note the options shown on the right of the dialog window.
For simplicity, select the authors table. Note the options shown on the right of the dialog window. These options show the various options in which synchronization will occur when the data on the server and client changes. Click OK. Then, back in the Configure Data Synchronization dialog, click OK again (see Figure 3).
The Generate SQL Scripts dialog will now appear. Click OK. A set of SQL scripts will now be generated and executed to modify the databases for synchronization. Next, the Data Source Configuration Wizard dialog will appear.
Expand the Tables item and check the authors table (see Figure 4). This will create a strongly typed Dataset that you can use in your application later on. Click Finish.
Figure 4. The Data Source Configuration Wizard: Check the table you want to use.
Figure 5. Solution Explorer: New items added to the project.
If you look at the Solution Explorer now, you will see that a number of items have been added to your project (see Figure 5).
These items are:
|Figure 6. Database Changes: Observe in Server Explorer the databases created/modified.|
- Various Library References: Microsoft.Synchronization.Data, Microsoft.Synchronization.Data.Server, and Microsoft.Synchronization.Data.SqlServerCe.
- Two SQL Scripts: LocalAuthors.dbo.authors.sql modifies the pubs database to track changes, while the LocalAuthors.dbo.authors.undo.sql script undoes the changes.
- LocalAuthors.Sync and LocalAuthors.Designer.cs: These files contain the code to perform the actual synchronizations.
- pubsDataSet.xsd: This strongly typed Dataset is created for use in the application.
At this point, you can see the created or modified databases in Server Explorer (Figure 6
The newly created pubs.sdf database contains all the fields that you selected earlier. In addition, it has two tracking columns: LastEditDate and CreationDate. For the server database, notice that the authors table also has two additional columns: LastEditDate and CreationDate. It also has three triggers added: authors_DeletionTrigger, authors_InsertTrigger, and authors_UpdateTrigger.
One additional table is added to the server: authors_Tombstone. This is for tracking deletions in the authors table.
Now, bind the pubs.sdf database to some controls on the Windows form. When you run the form, you'll make some changes to the data, and then synchronize those changes with the server.