我一直在使用SYNC Framework来合并2个不同的数据库。如果我的数据库是相同的列,那么它很简单。我可以轻松匹配并同步彼此。但是,如果我添加和删除表中的一些列,这些列有一些不常见的列。同步无法捕获列也匹配不起作用。 请不要给我文章链接。我一直在读所有这些。
我的愿望如下:
我的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
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();
}
}
}
}
答案 0 :(得分:1)