Programming Common Synchronization Tasks
The following topics in this section provide background information, procedural content, and complete code examples for common tasks that you can perform by using Microsoft Synchronization Services for ADO.NET. We recommend that you read the how-to topic about tracking changes in the server database first, and then read at least one of the topics about synchronization direction before you read the other topics.
In This Section
- Setup Scripts for Synchronization Services How-to Topics
- Utility Class for Synchronization Services How-to Topics
- Tracking Changes in the Server Database
- How to: Specify Snapshot, Download, Upload, and Bidirectional Synchronization
- How to: Use Session Variables
- How to: Initialize the Client Database and Work with Table Schema
- How to: Filter Rows and Columns
- How to: Work with Events and Program Business Logic
- How to: Handle Data Conflicts and Errors
- How to: Create Views of Data on a Client
- How to: Specify the Order and Batch Size of Changes
- How to: Configure N-Tier Synchronization
- How to: Configure Synchronization for a Device
- How to: Trace the Synchronization Process
- Walkthrough: Extending the Local Database Cache to Support Bidirectional Synchronization
Example Applications in the How-to Topics
Each how-to topic contains at least one console application. This application is written in both C# and Visual Basic and demonstrates particular features of the API. All the applications include the code to synchronize one time, change the client and server as appropriate, and then synchronize again. Although it is quicker to show only code examples for a specific task or feature, it is much easier to understand how Synchronization Services works if you see a particular feature in a fairly complete context.
Each application contains the following classes, and also other classes that are relevant to the features that are being described:
Program. This class sets up synchronization and calls methods from the
Utilityclass handles all functionality that is not directly related to synchronization, such as holding connection string information and making changes to the server and client databases.
SampleSyncAgent. This class is derived from SyncAgent.
SampleServerSyncProvider. This class is derived from DbServerSyncProvider. In the examples in which the server provider commands are relevant, the commands are specified manually. In other cases, the SqlSyncAdapterBuilder class is used because it requires less code. For more information about server provider commands, see How to: Specify Snapshot, Download, Upload, and Bidirectional Synchronization.
SampleClientSyncProvider. This class is derived from SqlCeClientSyncProvider.
All the complete applications are two-tier to avoid having to use a service to run the applications. However, the code is divided into separate classes so that it is easier to see how to apply code to N-tier scenarios. For more information about N-tier applications, see How to: Configure N-Tier Synchronization and the samples at this Microsoft Web site.
The code for all the applications has been tested on Visual Studio 2008, SQL Server Compact 3.5 Service Pack 1, SQL Server 2005 Service Pack 2, and SQL Server 2008.
To run each application
- Execute the Transact-SQL script in Setup Scripts for Synchronization Services How-to Topics. This script creates a database, a set of tables and triggers, and a stored procedure:
usp_InsertSampleData. Use this procedure if you want to delete and reinsert sample data.
- Copy the
Utilityclass code from Utility Class for Synchronization Services How-to Topics. Paste this code into each application that you want to run, or create a DLL from the code and reference that DLL from each application.
- Add references to the following DLLs: