Sdílet prostřednictvím


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

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 Utility class. The Utility class 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

  1. 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.
  2. Copy the Utility class 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.
  3. Add references to the following DLLs:
    • Microsoft.Synchronization.Data.dll
    • Microsoft.Synchronization.Data.Server.dll
    • Microsoft.Synchronization.Data.SqlServerCe.dll
    • System.Data.SqlServerCe.dll

See Also

Concepts

Synchronization Services for ADO.NET 1.0 SP1