Programming Common Client and Server Synchronization Tasks
备注
The following information is included as reference for existing applications that support offline scenarios only. For developing new applications, please see Architecture and Classes for Database Synchronization.
The following topics in this section provide background information, procedural content, and complete code examples for common client and server synchronization tasks that you can perform by using Sync Framework. 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
How to: Specify Snapshot, Download, Upload, and Bidirectional Synchronization
How to: Initialize the Client Database and Work with Table Schema
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 Sync Framework 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.
The code for all the applications has been tested on Visual Studio 2008, SQL Server Compact Service Pack 1, SQL Server 2005 Service Pack 2, and SQL Server 2008.
To run each application
Execute the "Custom Change Tracking for Offline Scenarios" Transact-SQL script in Setup Scripts for Database Provider How-to Topics. This script creates a database, a set of tables and triggers, and a set of stored procedures. To delete and reinsert sample data, use the usp_InsertSampleData stored procedure.
Copy the Utility class code from Utility Class for Database Provider 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:
Microsoft.Synchronization.dll
Microsoft.Synchronization.Data.dll
Microsoft.Synchronization.Data.Server.dll
Microsoft.Synchronization.Data.SqlServerCe.dll
System.Data.SqlServerCe.dll