Đồng bộ giữa Server và Client là vấn đề được bàn đến từ lâu, nhất là lập trình phần mềm chạy dữ liệu trên Internet. Nhưng khi chạy trực tiếp dữ liệu trên Internet sẽ có nhưng yếu điểm sau:
1. Tốc độ thực hiện chậm do hạn chế của băng thông Internet
2. Không phải lúc nào đường kết nối Internet cũng ổn định
3. Sai sót trong xử lý. Khi thực hiện gián tiếp trên File Local bạn sẽ định được 1 khoảng thời gian trước khi đồng bộ với Server
Hiện tại Microsoft đưa ra 1 giải pháp đó là Sync Framework. Phần mềm giúp đồng bộ giữa những CSDL khác nhau. và đây là cách thức đồng bộ hóa:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using System.Data;
using System.Data.SqlServerCe;
using Microsoft.Synchronization.Data.SqlServerCe;
using Microsoft.Synchronization;
using System.IO;
namespace Sync
{
class Program
{
static void Main(string[] args)
{
var file = "C:\\e.sdf";
var constrclient = String.Format(@"Data Source = {0}", file);
var constrserver = "Data Source=localhost/SQLEXPRESS;Initial Catalog=phanmem1;Persist Security Info=True;User ID=sa;Password=123456";
//Kết nối tại Server(MSSQL 2008)
SqlConnection serverConn = new SqlConnection(constrserver);
//Kết nối tại Client (SQLCE3.5)
SqlCeConnection clientConn = new SqlCeConnection(constrclient);
//Server Sync Scope
var scopeServer = new DbSyncScopeDescription("scope");
var serverProvision = new SqlSyncScopeProvisioning(serverConn);
//Nếu chưa có thì tạo
if (!serverProvision.ScopeExists("scope"))
{
//Duyệt tất cả Table để tạo
foreach (DataRow item in GetDatabaseTables_SQL(constrserver).Rows)
{
var name = item["Name"].ToString();
if (!name.EndsWith("_tracking")
&& name != "scope_info" && name != "scope_config" && name != "schema_info")
{
DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(name, serverConn);
scopeServer.Tables.Add(tableDesc);
}
}
serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
serverProvision.Apply();
}
//Client Sync Scope
//Nếu chưa có File thì tạo
if (!File.Exists(file))
{
using (SqlCeEngine sqlCeEngine = new SqlCeEngine(constrclient))
{
sqlCeEngine.CreateDatabase();
}
//Lấy Server Sync Scope
scopeServer = SqlSyncDescriptionBuilder.GetDescriptionForScope("scope", serverConn);
SqlCeSyncScopeProvisioning clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeServer);
clientProvision.Apply();
}
//Thực hiện đồng bộ hóa
SyncOrchestrator syncOrchestrator = new SyncOrchestrator()
{
Direction = SyncDirectionOrder.UploadAndDownload,
LocalProvider = new SqlCeSyncProvider("scope", clientConn),
RemoteProvider = new SqlSyncProvider("scope", serverConn),
};
//Theo dõi tiến trình xảy ra
((SqlCeSyncProvider)syncOrchestrator.LocalProvider).SyncProgress += (s, e) =>
{
Console.WriteLine("Thực hiện trên bảng: " + e.TableProgress.TableName);
};
var sync = syncOrchestrator.Synchronize();
}
private static DataTable GetDatabaseTables_SQL(string strConn)
{
DataTable tables = new DataTable("Tables");
using (SqlConnection connection =
new SqlConnection(strConn))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select table_name as Name from INFORMATION_SCHEMA.Tables where TABLE_TYPE = 'BASE TABLE'";
connection.Open();
tables.Load(command.ExecuteReader(
CommandBehavior.CloseConnection));
}
return tables;
}
}
}