SqlCeClientSyncProvider Class

Abstracts a client synchronization provider for SQL Server Compact that communicates with the client and shields the synchronization agent from the specific implementation of the client database.

Namespace: Microsoft.Synchronization.Data.SqlServerCe
Assembly: Microsoft.Synchronization.Data.SqlServerCe (in microsoft.synchronization.data.sqlserverce.dll)

Syntax

'Declaration
Public Class SqlCeClientSyncProvider
    Inherits ClientSyncProvider
public class SqlCeClientSyncProvider : ClientSyncProvider
public ref class SqlCeClientSyncProvider : public ClientSyncProvider
public class SqlCeClientSyncProvider extends ClientSyncProvider
public class SqlCeClientSyncProvider extends ClientSyncProvider

Remarks

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

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

  • Retrieves changes that occurred in the client database since the last synchronization.

  • Applies incremental changes to the client database.

  • Detects conflicting changes.

Inheritance Hierarchy

System.Object
   Microsoft.Synchronization.SyncProvider
     Microsoft.Synchronization.Data.ClientSyncProvider
      Microsoft.Synchronization.Data.SqlServerCe.SqlCeClientSyncProvider

Example

The following code example creates a class that derives from SqlCeClientSyncProvider. The class creates a connection to the client database and handles several common events, including schema creation in the client database. To view this code in the context of a complete example, see How to: Work with Events and Program Business Logic.

public class SampleClientSyncProvider : SqlCeClientSyncProvider
{

    public SampleClientSyncProvider()
    {
        //Specify a connection string for the sample client database.
        Utility util = new Utility();
        this.ConnectionString = util.ClientConnString;

        //Log information for the following events.
        this.SchemaCreated += new EventHandler<SchemaCreatedEventArgs>(EventLogger.LogEvents);
        this.ChangesSelected += new EventHandler<ChangesSelectedEventArgs>(EventLogger.LogEvents);
        this.ChangesApplied += new EventHandler<ChangesAppliedEventArgs>(EventLogger.LogEvents);
        this.ApplyChangeFailed += new EventHandler<ApplyChangeFailedEventArgs>(EventLogger.LogEvents);
        
        //Use the following events to fix up schema on the client.
        //We use the CreatingSchema event to change the schema
        //by using the API. We use the SchemaCreated event 
        //to change the schema by using SQL.
        //Note that both schema events fire for the Customer table,
        //even though we already created the table. This allows us
        //to work with the table at this point if we have to.
        this.CreatingSchema += new EventHandler<CreatingSchemaEventArgs>(SampleClientSyncProvider_CreatingSchema);
        this.SchemaCreated += new EventHandler<SchemaCreatedEventArgs>(SampleClientSyncProvider_SchemaCreated);

     }

    private void SampleClientSyncProvider_CreatingSchema(object sender, CreatingSchemaEventArgs e)
    {

        string tableName = e.Table.TableName;

        if (tableName == "Customer")
        {
            //Set the RowGuid property because it is not copied
            //to the client by default. This is also a good time
            //to specify literal defaults with .Columns[ColName].DefaultValue,
            //but we will specify defaults like NEWID() by calling
            //ALTER TABLE after the table is created.
            e.Schema.Tables["Customer"].Columns["CustomerId"].RowGuid = true;
        }

        if (tableName == "OrderHeader")
        {
            e.Schema.Tables["OrderHeader"].Columns["OrderId"].RowGuid = true;
        }
    }

    private void SampleClientSyncProvider_SchemaCreated(object sender, SchemaCreatedEventArgs e)
    {
        string tableName = e.Table.TableName;
        Utility util = new Utility();

        //Call ALTER TABLE on the client. This must be done
        //over the same connection and within the same
        //transaction that Synchronization Services uses
        //to create the schema on the client.
        if (tableName == "Customer")
        {
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "Customer");                
        }
       
        if (tableName == "OrderHeader")
        {
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "OrderHeader");                
        }

        if (tableName == "OrderDetail")
        {
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "OrderDetail");                
        }
    }
}
Public Class SampleClientSyncProvider
    Inherits SqlCeClientSyncProvider


    Public Sub New()
        'Specify a connection string for the sample client database.
        Dim util As New Utility()
        Me.ConnectionString = util.ClientConnString

        'Log information for the following events.
        AddHandler Me.SchemaCreated, AddressOf EventLogger.LogEvents
        AddHandler Me.ChangesSelected, AddressOf EventLogger.LogEvents
        AddHandler Me.ChangesApplied, AddressOf EventLogger.LogEvents
        AddHandler Me.ApplyChangeFailed, AddressOf EventLogger.LogEvents

        'Use the following events to fix up schema on the client.
        'We use the CreatingSchema event to change the schema
        'by using the API. We use the SchemaCreated event 
        'to change the schema by using SQL.
        'Note that both schema events fire for the Customer table,
        'even though we already created the table. This allows us
        'to work with the table at this point if we have to.
        AddHandler Me.CreatingSchema, AddressOf SampleClientSyncProvider_CreatingSchema
        AddHandler Me.SchemaCreated, AddressOf SampleClientSyncProvider_SchemaCreated

    End Sub 'New


    Private Sub SampleClientSyncProvider_CreatingSchema(ByVal sender As Object, ByVal e As CreatingSchemaEventArgs)

        Dim tableName As String = e.Table.TableName

        If tableName = "Customer" Then
            'Set the RowGuid property because it is not copied
            'to the client by default. This is also a good time
            'to specify literal defaults with .Columns[ColName].DefaultValue,
            'but we will specify defaults like NEWID() by calling
            'ALTER TABLE after the table is created.
            e.Schema.Tables("Customer").Columns("CustomerId").RowGuid = True
        End If

        If tableName = "OrderHeader" Then
            e.Schema.Tables("OrderHeader").Columns("OrderId").RowGuid = True
        End If

    End Sub 'SampleClientSyncProvider_CreatingSchema


    Private Sub SampleClientSyncProvider_SchemaCreated(ByVal sender As Object, ByVal e As SchemaCreatedEventArgs)
        Dim tableName As String = e.Table.TableName
        Dim util As New Utility()

        'Call ALTER TABLE on the client. This must be done
        'over the same connection and within the same
        'transaction that Synchronization Services uses
        'to create the schema on the client.
        If tableName = "Customer" Then
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "Customer")
        End If

        If tableName = "OrderHeader" Then
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "OrderHeader")
        End If

        If tableName = "OrderDetail" Then
            util.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "OrderDetail")
        End If

    End Sub 'SampleClientSyncProvider_SchemaCreated
End Class 'SampleClientSyncProvider

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.

Platforms

Development Platforms

For a list of the supported platforms, see Hardware and Software Requirements (Synchronization Services).

Target Platforms

See Also

Reference

SqlCeClientSyncProvider Members
Microsoft.Synchronization.Data.SqlServerCe Namespace