SyncAdapter Class
Assembly: Microsoft.Synchronization.Data.Server (in microsoft.synchronization.data.server.dll)
The SyncAdapter serves as a bridge between the DbServerSyncProvider and the server database. Modeled after the data adapter in ADO.NET, the synchronization adapter is defined for each table that is synchronized. The synchronization adapter provides the server synchronization provider with the specific commands that are required to interact with the server database, such as the InsertCommand that applies inserts from the client database to the server database. Because synchronization adapters use the ADO.NETDbCommand object, you can use any command structure that is supported by ADO.NET. This includes inline Transact-SQL, stored procedures, views, functions, and so on. The commands only require a single result that defines the structure and data to be transferred and applied.
The following code example creates a SyncAdapter object for the Customer table. The table is configured for bidirectional synchronization with conflict detection; therefore, all the available commands are specified. The commands for selecting changes from the server are inline Transact-SQL. The commands for applying changes to the server are stored procedures, which are defined in Setup Scripts for Synchronization Services How-to Topics. For more information about adapter commands, see How to: Specify Snapshot, Download, Upload, and Bidirectional Synchronization. To view this code in the context of a complete example, see How to: Handle Data Conflicts and Errors.
Dim customerSyncAdapter As New SyncAdapter("Customer") 'This command is used if @sync_row_count returns '0 when changes are applied to the server. Dim customerUpdateConflicts As New SqlCommand() With customerUpdateConflicts .CommandText = _ "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _ & "FROM Sales.Customer " + "WHERE CustomerId = @CustomerId" .Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier) .Connection = serverConn End With customerSyncAdapter.SelectConflictUpdatedRowsCommand = customerUpdateConflicts 'This command is used if the server provider cannot find 'a row in the base table. Dim customerDeleteConflicts As New SqlCommand() With customerDeleteConflicts .CommandText = _ "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _ & "FROM Sales.Customer_Tombstone " + "WHERE CustomerId = @CustomerId" .Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier) .Connection = serverConn End With customerSyncAdapter.SelectConflictDeletedRowsCommand = customerDeleteConflicts 'Select inserts from the server. Dim customerIncrInserts As New SqlCommand() With customerIncrInserts .CommandText = _ "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _ & "FROM Sales.Customer " _ & "WHERE (InsertTimestamp > @sync_last_received_anchor " _ & "AND InsertTimestamp <= @sync_new_received_anchor " _ & "AND InsertId <> @sync_client_id)" .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) .Connection = serverConn End With customerSyncAdapter.SelectIncrementalInsertsCommand = customerIncrInserts 'Apply inserts to the server. Dim customerInserts As New SqlCommand() customerInserts.CommandType = CommandType.StoredProcedure customerInserts.CommandText = "usp_CustomerApplyInsert" customerInserts.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) customerInserts.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit) customerInserts.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output customerInserts.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier) customerInserts.Parameters.Add("@CustomerName", SqlDbType.NVarChar) customerInserts.Parameters.Add("@SalesPerson", SqlDbType.NVarChar) customerInserts.Parameters.Add("@CustomerType", SqlDbType.NVarChar) customerInserts.Connection = serverConn customerSyncAdapter.InsertCommand = customerInserts 'Select updates from the server. Dim customerIncrUpdates As New SqlCommand() With customerIncrUpdates .CommandText = _ "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _ & "FROM Sales.Customer " _ & "WHERE (UpdateTimestamp > @sync_last_received_anchor " _ & "AND UpdateTimestamp <= @sync_new_received_anchor " _ & "AND UpdateId <> @sync_client_id " _ & "AND NOT (InsertTimestamp > @sync_last_received_anchor " _ & "AND InsertId <> @sync_client_id))" .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) .Connection = serverConn End With customerSyncAdapter.SelectIncrementalUpdatesCommand = customerIncrUpdates 'Apply updates to the server. Dim customerUpdates As New SqlCommand() customerUpdates.CommandType = CommandType.StoredProcedure customerUpdates.CommandText = "usp_CustomerApplyUpdate" customerUpdates.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp) customerUpdates.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) customerUpdates.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit) customerUpdates.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output customerUpdates.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier) customerUpdates.Parameters.Add("@CustomerName", SqlDbType.NVarChar) customerUpdates.Parameters.Add("@SalesPerson", SqlDbType.NVarChar) customerUpdates.Parameters.Add("@CustomerType", SqlDbType.NVarChar) customerUpdates.Connection = serverConn customerSyncAdapter.UpdateCommand = customerUpdates 'Select deletes from the server. Dim customerIncrDeletes As New SqlCommand() With customerIncrDeletes .CommandText = _ "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _ & "FROM Sales.Customer_Tombstone " _ & "WHERE (@sync_initialized = 1 " _ & "AND DeleteTimestamp > @sync_last_received_anchor " _ & "AND DeleteTimestamp <= @sync_new_received_anchor " _ & "AND DeleteId <> @sync_client_id)" .Parameters.Add("@" + SyncSession.SyncInitialized, SqlDbType.Bit) .Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncNewReceivedAnchor, SqlDbType.Timestamp) .Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) .Connection = serverConn End With customerSyncAdapter.SelectIncrementalDeletesCommand = customerIncrDeletes 'Apply deletes to the server. Dim customerDeletes As New SqlCommand() customerDeletes.CommandType = CommandType.StoredProcedure customerDeletes.CommandText = "usp_CustomerApplyDelete" customerDeletes.Parameters.Add("@" + SyncSession.SyncLastReceivedAnchor, SqlDbType.Timestamp) customerDeletes.Parameters.Add("@" + SyncSession.SyncClientId, SqlDbType.UniqueIdentifier) customerDeletes.Parameters.Add("@" + SyncSession.SyncForceWrite, SqlDbType.Bit) customerDeletes.Parameters.Add("@" + SyncSession.SyncRowCount, SqlDbType.Int).Direction = ParameterDirection.Output customerDeletes.Parameters.Add("@CustomerId", SqlDbType.UniqueIdentifier) customerDeletes.Connection = serverConn customerSyncAdapter.DeleteCommand = customerDeletes 'Add the SyncAdapter to the server synchronization provider. Me.SyncAdapters.Add(customerSyncAdapter)