如何修复数据库同步

时间:2019-07-10 16:02:03

标签: c#

我有两个具有两个不同连接的数据库(a和b)。尽管这两个数据库都在我的系统上,并且它们具有相同的表。如何将a和b一起同步?

这是C#代码

public void fromOfflineServer()
{
    try
    {
        SqlConnection serverConn = new SqlConnection(serverConnection);
        SqlConnection clientConn = new SqlConnection(clientConnection);
        //Drop scope_Info Table
        string cmdText = @"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES 
                   WHERE TABLE_NAME='scope_info') DROP table scope_info";
        clientConn.Open();
        SqlCommand cmd = new SqlCommand(cmdText, clientConn);
        cmd.ExecuteScalar();
        clientConn.Close();
        List<string> tables = new List<string>();
        tables.Add("AuditTrail");
        tables.Add("administrator");
        tables.Add("SQLAuditChanges");

        var scopeDesc = new DbSyncScopeDescription("state");
        foreach (var tbl in tables) //Add Tables in Scope
        {
            scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(tbl, clientConn));
        }
        SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc); //Provisioning
                                                                                                        //skip creating the user tables
        clientProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
        //skip creating the change tracking tables
        clientProvision.SetCreateTrackingTableDefault(DbSyncCreationOption.Skip);
        //skip creating the change tracking triggers
        clientProvision.SetCreateTriggersDefault(DbSyncCreationOption.Skip);
        //skip creating the insert/update/delete/selectrow SPs including those for metadata
        clientProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip);
        //create new SelectChanges SPs for selecting changes for the new scope
        //the new SelectChanges SPs will have a guid suffix
        clientProvision.SetCreateProceduresForAdditionalScopeDefault(DbSyncCreationOption.Create);
        clientProvision.Apply();
    }
    catch (SqlException ex)
    {
        DisplayError(ex.Message.ToString());
    }
}
public void fromOnlineServer()
{

    try
    {
        SqlConnection serverConn = new SqlConnection(serverConnection);

        string cmdText = @"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES 
                   WHERE TABLE_NAME='scope_info') DROP table scope_info";
        serverConn.Open();
        SqlCommand cmd = new SqlCommand(cmdText, serverConn);
        cmd.ExecuteScalar();
        serverConn.Close();
        List<string> tables = new List<string>();
        tables.Add("AuditTrail");
        tables.Add("administrator");
        tables.Add("SQLAuditChanges");
        var scopeDesc = new DbSyncScopeDescription("state");
        foreach (var tbl in tables)
        {
            scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(tbl, serverConn));
        }
        SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);

        serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
        //skip creating the change tracking tables
        serverProvision.SetCreateTrackingTableDefault(DbSyncCreationOption.Skip);
        //skip creating the change tracking triggers
        serverProvision.SetCreateTriggersDefault(DbSyncCreationOption.Skip);
        //skip creating the insert/update/delete/selectrow SPs including those for metadata
        serverProvision.SetCreateProceduresDefault(DbSyncCreationOption.Skip);
        serverProvision.Apply();
    }
    catch (SqlException ex)
    {
        DisplayError(ex.Message.ToString());
    }
}
public void doSynchronise()
{
    try
    {
        SqlConnection serverConn = new SqlConnection(serverConnection);
        SqlConnection clientConn = new SqlConnection(clientConnection);
        SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
        syncOrchestrator.LocalProvider = new SqlSyncProvider(sScope, clientConn);
        syncOrchestrator.RemoteProvider = new SqlSyncProvider(sScope, serverConn);
        syncOrchestrator.Direction = SyncDirectionOrder.Upload;
        ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
        SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
        DisplaySuccess("Start Time: " + syncStats.SyncStartTime);
        DisplaySuccess("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
        DisplaySuccess("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
        DisplaySuccess("Complete Time: " + syncStats.SyncEndTime);

    }
    catch (SqlException ex)
    {
        lblMessage.Text = ex.Message.ToString());
    }
}

我希望输出可以“成功同步”,但是输出是

  

System.InvalidOperationException:存储过程'[tbl_lga_selectchanges]'不存在。在System.Data.SqlClient.SqlCommand.DeriveParameters()在System.Data.SqlClient.SqlCommandBuilder.DeriveParameters(SqlCommand命令)在Microsoft.Synchronization.Data.ManagementUtils.DeriveParameters(SqlCommand命令)在Microsoft.Synchronization.Data.ManagementUtils.DeriveParameters (SqlCommand命令,IEnumerable`1列)位于c:\ webApplication \ appForms \ SynDatabase.aspx中SynDatabase.Sync()处Microsoft.Synchronization.SyncOrchestrator.Synchronize()处的[snip] Microsoft.Synchronization.KnowledgeSyncOrchestrator.Synchronize()处。 c:\ webApplication \ appForms \ SynDatabase.aspx.cs中的SynDatabase.startSynchronisation(Object sender,EventArgs e)处的cs:line 299

0 个答案:

没有答案