您可以搜索 .NET Sync Framework 使用此框架,您可以创建 3 种方法,
- 对于服务器机器或目标机器
- 对于客户端机器或源机器数据库
- 同步方法
根据您的选择,首先为客户端和服务器以及范围名称创建连接字符串。
static string sServerConnection =
@"Data Source=192.168.1.112;Initial Catalog=Server;User ID=sa;Password=123456";
static string sClientConnection =
@"Data Source=MAHAVEER;Initial Catalog=Client;Integrated Security=True";
static string sScope = "MainScope";
从客户端机器获取和存储数据
//Get Data From Client Provision
public static void ProvisionClient()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
SqlConnection clientConn = new SqlConnection(sClientConnection);
//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("Demo"); // Add Tables in List
tables.Add("Product");
var scopeDesc = new DbSyncScopeDescription("MainScope");
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();
}
在配置的帮助下将数据设置到服务器机器
//Set Data To Server Provision
public static void ProvisionServer()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
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("Demo");
tables.Add("Product");
var scopeDesc = new DbSyncScopeDescription("MainScope");
foreach (var tbl in tables)
{
scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(tbl, serverConn));
}
SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc); // Create Provision From All Tables
//skip creating the user tables
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();
}
上述两个进程同步进程将根据您的设置启动,SyncOrchestrator 类将负责所有同步进程,它是 Microsoft Sync Framework 的类
public static void Sync()
{
SqlConnection serverConn = new SqlConnection(sServerConnection);
SqlConnection clientConn = new SqlConnection(sClientConnection);
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();
Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
//Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
Console.WriteLine(String.Empty);
Console.ReadLine();
}
如果发生任何更改或错误,则此方法将返回。
static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
{
Console.WriteLine(e.Conflict.Type);
Console.WriteLine(e.Error);
}
而不是在main方法中调用以上三个方法。确保您必须以正确的方式调用这些方法,例如从客户端机器接收的第一个数据而不是设置为服务器机器,然后同步过程将开始
static void Main(string[] args)
{
ProvisionClient();
ProvisionServer();
Sync();
}
我希望这对你有帮助,我在一个项目中使用了这个代码,这对我来说很好。这仅适用于单个客户端和单机同步过程,不适用于多个。