SqlCeClientSyncProvider 类

针对 SQL Server Compact 对与客户端通信的客户端同步提供程序进行抽象化并将同步代理与客户端数据库的特定实现屏蔽开来。

命名空间: Microsoft.Synchronization.Data.SqlServerCe
程序集: Microsoft.Synchronization.Data.SqlServerCe(在 microsoft.synchronization.data.sqlserverce.dll 中)

语法

声明
<SuppressMessageAttribute("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")> _
<SuppressMessageAttribute("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase")> _
Public Class SqlCeClientSyncProvider
    Inherits ClientSyncProvider
用法
Dim instance As SqlCeClientSyncProvider
[SuppressMessageAttribute("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")] 
[SuppressMessageAttribute("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase")] 
public class SqlCeClientSyncProvider : ClientSyncProvider
[SuppressMessageAttribute(L"Microsoft.Maintainability", L"CA1506:AvoidExcessiveClassCoupling")] 
[SuppressMessageAttribute(L"Microsoft.Naming", L"CA1706:ShortAcronymsShouldBeUppercase")] 
public ref class SqlCeClientSyncProvider : public ClientSyncProvider
/** @attribute SuppressMessageAttribute("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling") */ 
/** @attribute SuppressMessageAttribute("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase") */ 
public class SqlCeClientSyncProvider extends ClientSyncProvider
SuppressMessageAttribute("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling") 
SuppressMessageAttribute("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase") 
public class SqlCeClientSyncProvider extends ClientSyncProvider

备注

客户端同步提供程序的主要活动如下所示:

  • 存储与客户端上支持同步的表有关的信息。

  • 检索自上次同步以来在客户端数据库中发生的变更。

  • 将增量变更应用于客户端数据库。

  • 检测发生冲突的变更。

示例

下面的代码示例创建一个派生自 SqlCeClientSyncProvider 的类。此类创建一个到客户端数据库的连接并处理多个公用事件,包括客户端数据库中的架构创建。若要在完整示例上下文中查看此代码,请参见如何处理事件和对业务逻辑进行编程

public class SampleClientSyncProvider : SqlCeClientSyncProvider
{

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

        //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 Sync Framework uses
        //to create the schema on the client.
        if (tableName == "Customer")
        {
            Utility.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "Customer");                
        }
       
        if (tableName == "OrderHeader")
        {
            Utility.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "OrderHeader");                
        }

        if (tableName == "OrderDetail")
        {
            Utility.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 = Utility.ConnStr_SqlCeClientSync

        '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 Sync Framework uses
        'to create the schema on the client.
        If tableName = "Customer" Then
            Utility.MakeSchemaChangesOnClient(e.Connection, e.Transaction, "Customer")
        End If

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

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

    End Sub 'SampleClientSyncProvider_SchemaCreated
End Class 'SampleClientSyncProvider

继承层次结构

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

线程安全

此类型的所有公共静态(在 Visual Basic 中共享 )成员都是线程安全的。不保证任何实例成员的线程安全。

请参阅

参考

SqlCeClientSyncProvider 成员
Microsoft.Synchronization.Data.SqlServerCe 命名空间