尝试运行数据库同步时出错

时间:2014-01-23 11:13:22

标签: c# sql sql-server-2008 synchronization microsoft-sync-framework

我正在尝试将3个SQL Server 2008表同步到SQL Server CE 3.5数据库。以下代码在底部生成错误。有什么想法吗?存在跟踪表以及存储过程。

由于

代码:

SyncLibrary.DatabaseSyncInfo inf = new SyncLibrary.DatabaseSyncInfo();
SynchronizationHelper hel = new SynchronizationHelper(inf);

inf.LocalConnectionString = @"Data Source=admin.domain.com,1435;Initial Catalog=ProjectAdmin;User ID=sa;Password=password";
inf.LocalDriverType = SyncLibrary.eSyncDriverType.SQLServer;

inf.RemoteConnectionString = @"Data Source=C:\Projects\Admin\TCPClient\ProjectAdmin.sdf";
inf.RemoteDriverType = SyncLibrary.eSyncDriverType.SQLCompact;
inf.ScopeName = "ProjectSCOPE";

System.Collections.Generic.List<string> tables = new System.Collections.Generic.List<string>();
tables.Add("Users");
tables.Add("SB_Questions");
tables.Add("Kiosks");

inf.SyncTables = tables;

SyncOperationStatistics ss = hel.DoSync();

Console.WriteLine("Start time: " + ss.SyncStartTime);
Console.WriteLine("Total changes uploaded: " + ss.UploadChangesTotal);
Console.WriteLine("Total changes downloaded: " + ss.DownloadChangesTotal);
Console.WriteLine("Complete time: " + ss.SyncEndTime);

public SyncOperationStatistics DoSync()
{
        SqlSyncProvider localProvider = ConfigureSqlServerSyncProvider(new SqlConnection(this.dbInfo.LocalConnectionString));

        RelationalSyncProvider remoteProvider = null;
        if (dbInfo.RemoteDriverType == eSyncDriverType.SQLServer)
        {
            remoteProvider = ConfigureSqlServerSyncProvider(new SqlConnection(this.dbInfo.RemoteConnectionString));
        }
        else if (dbInfo.RemoteDriverType == eSyncDriverType.SQLCompact)
        {
            remoteProvider = ConfigureSqlServerCeSyncProvider(new SqlCeConnection(this.dbInfo.RemoteConnectionString));
        }

        //Set memory data cache size property. 0 represents non batched mode
        localProvider.MemoryDataCacheSize = dbInfo.LocalBatchSize;
        remoteProvider.MemoryDataCacheSize = dbInfo.RemoteBatchSize;

        //Set batch spool location. Default value if not set is %Temp% directory.
        if (string.IsNullOrEmpty(dbInfo.LocalBatchSpoolFolder) == false)
        {
            localProvider.BatchingDirectory = dbInfo.LocalBatchSpoolFolder;
        }

        if (string.IsNullOrEmpty(dbInfo.RemoteBatchSpoolFolder) == false)
        {
            remoteProvider.BatchingDirectory = dbInfo.RemoteBatchSpoolFolder;
        }

        SyncOperationStatistics stats = this.SynchronizeProviders(localProvider, remoteProvider);

        //TimeSpan diff = stats.SyncEndTime.Subtract(stats.SyncStartTime);
        //Print Sync stats object
        //this.syncStats.Text = string.Format("Batching: {4} - Total Time To Synchronize = {0}:{1}:{2}:{3}",
        //    diff.Hours, diff.Minutes, diff.Seconds, diff.Milliseconds, (this._batchSize > 0) ? "Enabled" : "Disabled");
        //this.ReadTableValuesForSelectedTab();
        return stats;
    }

错误:

  

无法应用更改,因为本地提供程序没有为从远程提供程序接收的以下表配置适配器:Kiosks。确保已为Scope'ProjectSCOPE'的两个提供程序添加了正确的适配器,并且已正确配置任何表映射。

2 个答案:

答案 0 :(得分:1)

在我看来,客户端配置与服务器配置不同。我已取消配置数据库和应用配置。之后,此错误将被删除。

        try
        {
            SqlSyncScopeDeprovisioning obj1 = new SqlSyncScopeDeprovisioning(clientConn) { CommandTimeout = 60 * 30 };
            obj1.DeprovisionScope(scopeName);
        }
        catch { }

        // get the description of ProductsScope from the SyncDB server database
        DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(scopeName, serverConnection);

        SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc);

        if (!clientProvision.ScopeExists(scopeName))
        {
            //apply the scope definition
            clientProvision.Apply();
        }

答案 1 :(得分:0)

看起来远程数据库和本地数据库中的表名不匹配。

尝试在TableDescription上使用GlobalName属性。例如,如果有两个名为Countries_Version和Countries的表,但它们确实公开了相同的数据,唯一的区别是名称,您可以执行类似下面的操作,通过GlobalName属性进行映射。

        DbSyncTableDescription CountriesTableDescription;
        if (provider.Connection.Database.ToLower() == "peer1")
            CountriesTableDescription = SqlSyncDescriptionBuilder.GetDescriptionForTable( "Countries_Version", (System.Data.SqlClient.SqlConnection)provider.Connection );
        else
            CountriesTableDescription = SqlSyncDescriptionBuilder.GetDescriptionForTable( "Countries", (System.Data.SqlClient.SqlConnection)provider.Connection );

        // map to the Countries_Version table on peer2
        CountriesTableDescription.GlobalName = "Countries";