在Sync Framework中配置适配器

时间:2016-08-11 20:49:40

标签: c# sql-server synchronization

使用Microsoft的Sync Framework Tool,我配置了两个Microsoft SQL Server数据库(第一个代码片段)。接下来,为了尝试同步它们中的数据,我从MSDN的教程中运行了一些代码,这些代码我已经量身定制,以适应我的数据库,这些代码都很简单,而且根本不复杂(第二个代码片段)。

我的问题是,当我运行同步代码时,出现错误:

  

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

我还会包含一些数据库表的片段,以表明服务器配置似乎已成功。

服务器配置

//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 Snippets

enter image description here

1 个答案:

答案 0 :(得分:0)

所以问题就在于Database1和Database2中的表与名称不匹配,这在某种程度上很重要。我自己并不完全理解它,但显然同步的表的名称必须与它们的架构相匹配。