Share via


DbServerSyncProvider Class

Abstracts a generic server synchronization provider that communicates with the server database and shields the synchronization agent from the specific implementation of the database.

Inheritance Hierarchy

System.Object
  Microsoft.Synchronization.SyncProvider
    Microsoft.Synchronization.Data.ServerSyncProvider
      Microsoft.Synchronization.Data.Server.DbServerSyncProvider

Namespace:  Microsoft.Synchronization.Data.Server
Assembly:  Microsoft.Synchronization.Data.Server (in Microsoft.Synchronization.Data.Server.dll)

Syntax

'Declaration
Public Class DbServerSyncProvider _
    Inherits ServerSyncProvider _
    Implements IDisposable
'Usage
Dim instance As DbServerSyncProvider
public class DbServerSyncProvider : ServerSyncProvider, 
    IDisposable
public ref class DbServerSyncProvider : public ServerSyncProvider, 
    IDisposable
type DbServerSyncProvider =  
    class
        inherit ServerSyncProvider
        interface IDisposable
    end
public class DbServerSyncProvider extends ServerSyncProvider implements IDisposable

The DbServerSyncProvider type exposes the following members.

Constructors

  Name Description
Public method DbServerSyncProvider Initializes a new instance of the DbServerSyncProvider class.

Top

Properties

  Name Description
Public property BatchSize Gets or sets the batch size (in rows) that is used by commands that retrieve changes from the server database.
Public property Connection Gets or sets an IDbConnection object that is used to connect to the server database.
Public property Schema Gets or sets a SyncSchema object that contains information about the table schema on the server.
Public property SelectClientIdCommand Gets or sets an IDbCommand object that contains the query or stored procedure that returns originator IDs from the server database.
Public property SelectNewAnchorCommand Gets or sets an IDbCommand object that contains the query or stored procedure that returns a new anchor value from the server database. The anchor defines the upper bound for the set of changes to be synchronized during the current session.
Public property SyncAdapters Gets the SyncAdapterCollection that is associated with the DbServerSyncProvider.

Top

Methods

  Name Description
Public method ApplyChanges Applies inserts, updates, and deletes for a synchronization group to the server database. (Overrides ServerSyncProvider.ApplyChanges(SyncGroupMetadata, DataSet, SyncSession).)
Public method Dispose() Releases all resources used by the DbServerSyncProvider. (Overrides ServerSyncProvider.Dispose().)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the DbServerSyncProvider and optionally releases the managed resources.
Public method Equals (Inherited from Object.)
Protected method Finalize (Inherited from Object.)
Public method GetChanges Selects for a table in the server database the inserts, updates, and deletes to apply to the client database for a synchronization group. (Overrides ServerSyncProvider.GetChanges(SyncGroupMetadata, SyncSession).)
Public method GetHashCode (Inherited from Object.)
Public method GetSchema Returns a SyncSchema object that contains the schema for each table specified. (Overrides ServerSyncProvider.GetSchema(Collection<String>, SyncSession).)
Public method GetServerInfo Gets a SyncServerInfo object when give a session parameter. (Overrides ServerSyncProvider.GetServerInfo(SyncSession).)
Public method GetType (Inherited from Object.)
Protected method MemberwiseClone (Inherited from Object.)
Protected method OnApplyChangeFailed Raises the ApplyChangeFailed event.
Protected method OnApplyingChanges Raises the ApplyingChanges event.
Protected method OnChangesApplied Raises the ChangesApplied event.
Protected method OnChangesSelected Raises the ChangesSelected event.
Protected method OnSelectingChanges Raises the SelectingChanges event.
Protected method OnSyncProgress Raises the SyncProgress event.
Public method ToString (Inherited from Object.)

Top

Events

  Name Description
Public event ApplyChangeFailed Occurs after a row fails to be applied at the server.
Public event ApplyingChanges Occurs before changes are applied at the server for a synchronization group.
Public event ChangesApplied Occurs after all changes are applied at the server for a synchronization group.
Public event ChangesSelected Occurs after all changes to be applied to the client for a synchronization group are selected from the server.
Public event SelectingChanges Occurs before all changes to be applied to the client for a synchronization group are selected from the server.
Public event SyncProgress Occurs during the selection and application of changes for a synchronization group at the server.

Top

Remarks

The principal activities of the server synchronization provider are as follows:

  • Stores information about tables on the server that are enabled for synchronization.

  • Enables applications to retrieve changes that occurred in the server database since the last synchronization.

  • Applies incremental changes to the server database.

  • Detects conflicting changes.

Examples

The following code example creates a class that derives from DbServerSyncProvider. The class creates a connection and commands to download changes for snapshot synchronization. To view this code in the context of a complete example, see How to: Download a Snapshot of Data to a Client.

public class SampleServerSyncProvider : DbServerSyncProvider
{
    public SampleServerSyncProvider()
    {
        //Create a connection to the sample server database.
        Utility util = new Utility();
        SqlConnection serverConn = new SqlConnection(Utility.ConnStr_DbServerSync);
        this.Connection = serverConn;

        //Create a SyncAdapter for each table, and then define
        //the command to select rows from the table. With the Snapshot
        //option, you do not download incremental changes. However,
        //you still use the SelectIncrementalInsertsCommand to select
        //the rows to download for each snapshot. The commands include
        //only those columns that you want on the client.

        //Customer table.
        SyncAdapter customerSyncAdapter = new SyncAdapter("Customer");
        SqlCommand customerIncrInserts = new SqlCommand();
        customerIncrInserts.CommandText = 
            "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " +
            "FROM Sales.Customer";
        customerIncrInserts.Connection = serverConn;
        customerSyncAdapter.SelectIncrementalInsertsCommand = customerIncrInserts;
        this.SyncAdapters.Add(customerSyncAdapter);

        //OrderHeader table.
        SyncAdapter orderHeaderSyncAdapter = new SyncAdapter("OrderHeader");
        SqlCommand orderHeaderIncrInserts = new SqlCommand();
        orderHeaderIncrInserts.CommandText = 
            "SELECT OrderId, CustomerId, OrderDate, OrderStatus " +
            "FROM Sales.OrderHeader";
        orderHeaderIncrInserts.Connection = serverConn;
        orderHeaderSyncAdapter.SelectIncrementalInsertsCommand = orderHeaderIncrInserts;
        this.SyncAdapters.Add(orderHeaderSyncAdapter);

        //OrderDetail table.
        SyncAdapter orderDetailSyncAdapter = new SyncAdapter("OrderDetail");
        SqlCommand orderDetailIncrInserts = new SqlCommand();            
        orderDetailIncrInserts.CommandText = 
            "SELECT OrderDetailId, OrderId, Product, Quantity " +
            "FROM Sales.OrderDetail";
        orderDetailIncrInserts.Connection = serverConn;
        orderDetailSyncAdapter.SelectIncrementalInsertsCommand = orderDetailIncrInserts;
        this.SyncAdapters.Add(orderDetailSyncAdapter);
    }
}
Public Class SampleServerSyncProvider
    Inherits DbServerSyncProvider

    Public Sub New()
        'Create a connection to the sample server database.
        Dim util As New Utility()
        Dim serverConn As New SqlConnection(Utility.ConnStr_DbServerSync)
        Me.Connection = serverConn

        'Create a SyncAdapter for each table, and then define
        'the command to select rows from the table. With the Snapshot
        'option, you do not download incremental changes. However,
        'you still use the SelectIncrementalInsertsCommand to select
        'the rows to download for each snapshot. The commands include
        'only those columns that you want on the client.
        'Customer table.
        Dim customerSyncAdapter As New SyncAdapter("Customer")
        Dim customerIncrInserts As New SqlCommand()
        customerIncrInserts.CommandText = _
            "SELECT CustomerId, CustomerName, SalesPerson, CustomerType " _
          & "FROM Sales.Customer"
        customerIncrInserts.Connection = serverConn
        customerSyncAdapter.SelectIncrementalInsertsCommand = customerIncrInserts
        Me.SyncAdapters.Add(customerSyncAdapter)

        'OrderHeader table.
        Dim orderHeaderSyncAdapter As New SyncAdapter("OrderHeader")
        Dim orderHeaderIncrInserts As New SqlCommand()
        orderHeaderIncrInserts.CommandText = _
            "SELECT OrderId, CustomerId, OrderDate, OrderStatus " _
          & "FROM Sales.OrderHeader"
        orderHeaderIncrInserts.Connection = serverConn
        orderHeaderSyncAdapter.SelectIncrementalInsertsCommand = orderHeaderIncrInserts
        Me.SyncAdapters.Add(orderHeaderSyncAdapter)

        'OrderDetail table.
        Dim orderDetailSyncAdapter As New SyncAdapter("OrderDetail")
        Dim orderDetailIncrInserts As New SqlCommand()
        orderDetailIncrInserts.CommandText = _
            "SELECT OrderDetailId, OrderId, Product, Quantity " _
          & "FROM Sales.OrderDetail"
        orderDetailIncrInserts.Connection = serverConn
        orderDetailSyncAdapter.SelectIncrementalInsertsCommand = orderDetailIncrInserts
        Me.SyncAdapters.Add(orderDetailSyncAdapter)

    End Sub 'New
End Class 'SampleServerSyncProvider

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Synchronization.Data.Server Namespace