同步框架并同步到其他表名

时间:2018-08-27 06:59:16

标签: c# microsoft-sync-framework

我想将表从本地计算机同步到服务器,但名称不同。但是服务器中的表可能不存在,因此我希望将表创建为同步的一部分。

这是我当前的代码:

        try
        {
            using (SqlConnection sourceConn = new SqlConnection(ConfigurationManager.AppSettings["SourceSQLConnectionString"]))
            {
                sourceConn.Open();
                using (SqlConnection destConn = new SqlConnection(ConfigurationManager.AppSettings["DestinationSQLConnectionString"]))
                {
                    destConn.Open();
                    DbSyncScopeDescription myScope = new DbSyncScopeDescription(scopeName);

                    string[] columns = ConfigurationManager.AppSettings["ColumnsToSync"].Split(',').Where(a => !String.IsNullOrWhiteSpace(a)).Select(a => a.Trim()).ToArray();
                    Collection<string> columnsToSync = new Collection<string>();
                    foreach (string column in columns)
                    {
                        columnsToSync.Add(column);
                    }

                    DbSyncTableDescription tableToSync = SqlSyncDescriptionBuilder.GetDescriptionForTable(ConfigurationManager.AppSettings["TableToSync"], columnsToSync, sourceConn);
                    myScope.Tables.Add(tableToSync);

                    SqlSyncScopeProvisioning sourceProv = new SqlSyncScopeProvisioning(sourceConn, myScope);
                    if (!sourceProv.ScopeExists(scopeName))
                    {
                        sourceProv.Apply();
                        log.Info("Provisioned source database");
                    }

                    SqlSyncScopeProvisioning destProv = new SqlSyncScopeProvisioning(destConn, myScope);
                    if (!destProv.ScopeExists(scopeName))
                    {
                        destProv.Apply();
                        log.Info("Provisioned destination database");
                    }

                    destConn.Close();
                }
                sourceConn.Close();
            }
        }
        catch(Exception e)
        {
            log.Error($"Error occurred provisioning databases with message {e.Message}");
        }

这可行,并且正确创建了目标表,但是源表和目的表的名称相同。如何告诉同步框架同步到其他表名并创建表(如果该表不存在)?

0 个答案:

没有答案