【问题标题】:Configuring Adapters in Sync Framework在 Sync Framework 中配置适配器
【发布时间】:2016-08-11 20:49:40
【问题描述】:

使用 Microsoft 的 Sync Framework Tool,我提供了两个 Microsoft SQL Server 数据库(第一个代码 sn-p)。接下来,为了尝试同步其中的数据,我运行了 MSDN 教程中的一些代码,这些代码是我为适应我的数据库而量身定制的,这些代码既非常简单又一点也不复杂(第二个代码 sn-p)。

我的问题是,当我运行同步代码时,我得到一个错误:

无法应用更改,因为本地提供商没有适配器 为从远程接收到的下表配置 提供者:Spray_History。确保已安装正确的适配器 添加到范围“SprayHistory_SCOPE”的两个提供程序中,并且任何 表映射已正确配置。

我还将包括一些数据库表的 sn-ps,只是为了表明服务器配置似乎已经成功。


服务器配置

//Create a connection to the DustSuppression database (the Catalog name here changes for each database)
SqlConnection sqlConnection = new SqlConnection("Data Source = TKTEST-2; Initial Catalog = Dust_Suppression; Integrated Security = SSPI");

//Create a sync scope for SprayHistory table in database (we can name it whatever we want when we create it)
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("SprayHistory_SCOPE");

//Specify name of sync scope and list of tables to be synced (this needs to be the actual table name & server connection)
DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Spray_History", sqlConnection);

//Add the table description to the scope description
scopeDesc.Tables.Add(tableDesc);

//Provision the database with sync related artifacts (create provision object using scope description & server connection)
SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(sqlConnection, scopeDesc);

//Since the SprayHistory table already exists, inform the sync tool to skip creating it
serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

//Start the provisioning
serverProvision.Apply();

在服务器之间同步数据

static void Main(string[] args)
{
        //Connection string to the client (assume this to be S2)(this would normally be an ExpressDB)
        SqlConnection clientConnection = new SqlConnection("Data Source=TKTEST-2;Initial Catalog=Dust_Suppression;Integrated Security=SSPI");

        //Connection string to the database (assume this to be S1)(this would be the normal SQL DB)
        SqlConnection serverConnection = new SqlConnection("Data Source=TKTEST-2;Initial Catalog=DustSuppression;Integrated Security=SSPI");

        //Create a sync orchestrator
        SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

        //Set local provider of orchestrator to a sync provider (S2)(this would normally be an ExpressDB)
        syncOrchestrator.LocalProvider = new SqlSyncProvider("SprayHistory_SCOPE", clientConnection);

        //Set remote provider of orchestrator to a server sync provider (S1)(this would be the normal SQL DB)
        syncOrchestrator.RemoteProvider = new SqlSyncProvider("SprayHistory_SCOPE", serverConnection);

        //Set the direction of sync session to UPload and Download
        syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

        //Subscribe for errors that occur when applying changes to the client
        ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

        //Execute the synchronization process
        SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();

        // print statistics
        Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
        Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
        Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
        Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
        Console.WriteLine(String.Empty);
 }

 static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
 {
        // display conflict type
        Console.WriteLine(e.Conflict.Type);

        // display error message 
        Console.WriteLine(e.Error);
  }

SSMS 片段

【问题讨论】:

    标签: c# sql-server synchronization


    【解决方案1】:

    所以问题最终是 Database1 和 Database2 中的表名称不匹配,这出于某种原因很重要。我自己并不完全理解,但显然正在同步的表的名称及其架构必须匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      相关资源
      最近更新 更多