如何使用Sync FrameWork同步2个具有不同列的表?

时间:2014-04-17 12:56:27

标签: c# visual-studio synchronization sync microsoft-sync-framework

我一直在使用SYNC Framework来合并2个不同的数据库。如果我的数据库是相同的列,那么它很简单。我可以轻松匹配并同步彼此。但是,如果我添加和删除表中的一些列,这些列有一些不常见的列。同步无法捕获列也匹配不起作用。 请不要给我文章链接。我一直在读所有这些。

我的愿望如下:

http://jtabadero.wordpress.com/2011/08/19/part-4-synchronizing-tables-with-different-table-names-and-column-names/

enter image description here 我的SQL脚本:


GO

CREATE TABLE [dbo].[Customer](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [SurName] [nvarchar](50) NULL,
    [Age] [int] NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

USE [clientDb3]
GO

/****** Object:  Table [dbo].[Customer]    Script Date: 4/17/2014 10:15:45 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Customer](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [SurName] [nvarchar](50) NULL,
    [Description] [nvarchar](50) NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


C#:


namespace syncSample1
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverConn = @"data source=H2014-008\SQLEXPRESS;initial catalog=serverDb3;integrated security=True;MultipleActiveResultSets=True;";
            string clientConn = @"data source=H2014-008\SQLEXPRESS;initial catalog=clientDb3;integrated security=True;MultipleActiveResultSets=True;";

            using (var serverConnection = new SqlConnection(serverConn))
            using(var clientConnection = new SqlConnection(clientConn))
            {

                var provider1 = new SqlSyncProvider("scopeTest",serverConnection);
                var provider2 = new SqlSyncProvider("scopeTest",clientConnection);


                //config

                PrepareServerForProvisioning(provider1);
                PrepareClientForProvisioning(provider2, serverConnection);

               //sync
                SyncOrchestrator sync = new SyncOrchestrator();
                sync.LocalProvider= provider1;
                sync.RemoteProvider=provider2;
                sync.Direction = SyncDirectionOrder.UploadAndDownload;
                sync.Synchronize();

                Console.Read();

            }


        }

        private static void PrepareServerForProvisioning(SqlSyncProvider provider)
        {
            SqlConnection connection = (SqlConnection)provider.Connection;
            SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);
            if (!config.ScopeExists(provider.ScopeName))
            {
                DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
                scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", connection));
                DbSyncTableDescription serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", connection);
                serverTableDesc.GlobalName = "CustomerTable";
                config.PopulateFromScopeDescription(scopeDesc);
                config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
                config.Apply();

            }


        }

        private static void PrepareClientForProvisioning(SqlSyncProvider provider, SqlConnection sourceConnection)
        {
            SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning((SqlConnection)provider.Connection);
            if (!config.ScopeExists(provider.ScopeName))
            {
                DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(provider.ScopeName, sourceConnection);
                scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", sourceConnection));
                config.PopulateFromScopeDescription(scopeDesc);
                config.Apply();

            }

        }


    }
}

1 个答案:

答案 0 :(得分:1)

对于你想要达到的目标,并没有什么特别之处。非常支持开箱即用。配置表时,您可以指定要包含的列。您可以在代码中指定应为每个表添加哪些特定列。在上面的代码中,您使用GetDescriptionForTable来获取所有列。您可以保留相同的代码,只需删除要排除的列。这种方法在上面引用的博文中。