How to: Intercept and Change Data During Synchronization

This topic describes how to intercept and change data that is sent or received during synchronization. This technique can be used to enforce business rules by making changes to data as it goes in and out of a database. The code in this topic focuses on the following Sync Framework classes and events:

ChangesSelected

ChangesApplied

For more information about how to run the sample code, see "Example Applications in the How to Topics" in Synchronizing SQL Server and SQL Server Compact.

Understanding Business Rules

Many businesses have particular rules to apply to data that flows into and out of their databases. Because these rules are particular to your business, Sync Framework offers several events that you can use to intercept the data that flows during synchronization. By handling these events you can interject your own code to enforce whatever business rules you require. The following table lists the most common events to use for business rules, and when they occur during synchronization:

Event

When does it occur?

ChangesSelected

Occurs when the provider is the source provider, after enumerating changes but before sending the changes to the destination provider.

ApplyingChanges

Occurs when the provider is the destination provider, after connecting to the database but before changes are applied.

ChangesApplied

Occurs when the provider is the destination provider, after changes are applied but before committing the transaction and disconnecting from the database.

Be aware that the SqlSyncProvider class does not override these events, so when you use SqlSyncProvider you use the events directly from the RelationalSyncProvider base class. The SqlCeSyncProvider class does override these events, so when you use the SqlCeSyncProvider you use the overridden versions. The time when the events occur during synchronization is the same in both cases.

Logging Enumerated Changes in the ChangesSelected Event

Handling the ChangesSelected event allows you to examine the data that is about to be sent from a database during synchronization, before the data is sent. An example of an action to take in this event handler is to log the data that goes out of a server database so that you can later examine exactly what data was sent to which client databases. Alternately, you could send notification to another system, such as telling a warehouse to ship product when a sale is completed.

Before you can handle the event, you must register an event handler with the SqlSyncProvider class to handle the ChangesSelected event. The following code example shows how to do this:

The following code example shows how to handle the ChangesSelected event to log the data that is about to be sent.

Changing Data in the ChangesApplied Event

Handling the ChangesApplied event allows you to examine the data that has just been applied to the database, but before the transaction has been committed. An example of an action to take in this event handler is to fix up data that has been formatted incorrectly. Because a change made to data in this event occurs after the change from the client has been applied, the change is treated as a local change and is stamped with a version that indicates that it is not contained in the client database. Therefore, next time the client performs a download synchronization with the server, it will receive the fixed data.

Before you can handle the event, you must register an event handler with the SqlSyncProvider class to handle the ChangesApplied event. The following code example shows how to do this:

The following code example shows how to examine changed rows in a customer contact table and build a SQL UPDATE command that fixes the formatting of incorrectly formatted phone numbers. The change is made by using an IDbCommand object that uses the current connection and transaction, but makes the change outside of the Sync Framework API so that the change is treated as a local change and receives a new version stamp.

Complete Code Example

The following complete code example includes the code examples that are described earlier and additional code to configure and clean up the databases. The example requires the Utility class that is available in Utility Class for Database Provider How-to Topics.

Vea también

Conceptos

Advanced Synchronization Scenarios for SQL Server and SQL Server Compact