How to: Upload Incremental Data Changes to a Server

This topic shows you how to upload incremental data changes from a SQL Server Compact client database to a server database. The examples in this topic focus on the following Sync Framework types and events:

For information about how to run sample code, see "Example Applications in the How to Topics" in Programming Common Client and Server Synchronization Tasks.

The example code in this topic shows how to configure upload-only synchronization for the Customer table from the Sync Framework sample database. For an overview of the properties that are used to specify synchronization direction, see How to: Specify Snapshot, Download, Upload, and Bidirectional Synchronization.

Upload-only synchronization requires that incremental data changes be tracked in the client database and applied to the server database. Sync Framework handles the tracking in the client database. However, you must specify the commands that apply changes to the server database, as shown in the example code.

Key Parts of the API

This section provides code examples that point out the key parts of the API that are used in upload-only synchronization. The following code example specifies synchronization direction and how the table should be created on the client.

The following code example specifies a command to retrieve a new anchor value from the server. In this case, a timestamp value is retrieved and stored in the client database. During each synchronization session, the anchor value is used to ensure that uploaded changes do not overwrite changes at the server that occurred since the last synchronization session.

The following code example specifies a command to obtain the schema from the server database. Unlike other types of synchronization, with upload-only synchronization, this command does not obtain data from the server database.

The following code example specifies a command to apply updates from the client to the server. This is an UPDATE statement that includes the columns we want to update. There is a WHERE clause that uses the anchor value that the SelectNewAnchorCommand retrieves and the client ID. The WHERE clause also includes @sync_force_write = 1. This parameter forces conflicting updates to be applied. For more information about @sync_force_write and other session variables, see How to: Use Session Variables.

Complete Code Example

The following complete code example includes the code examples that are described earlier and additional code to perform synchronization. Also be aware of how SyncStatistics is used to display information about the synchronization session, and how the CreatingSchema and SchemaCreated events are used to add a default for the CustomerId column. The example requires the Utility class that is available in Utility Class for Database Provider How-to Topics.