2010年9月7日 星期二

Microsoft Sync Framework 2.0 實作簡單的 SQL Server 資料

引用處
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data;

namespace DataSync
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}

/*----- 資料同步按鈕的 Click 事件,處理資料同步 -----*/
private void btnSync_Click(object sender, EventArgs e)
{
// 定義資料庫連線
System.Data.SqlClient.SqlConnection clientConnection = new System.Data.SqlClient.SqlConnection("Server=localhost\\SQLEXPRESS;Initial Catalog=pos;Persist Security Info=True;User ID=YOURID;Password=YOURPWD");
System.Data.SqlClient.SqlConnection serverConnection = new System.Data.SqlClient.SqlConnection("Server=SQLSERVER;Initial Catalog=pos;Persist Security Info=True;User ID=YOURID;Password=YOURPWD");
string strMsg;

// 定義資料同步provider
SqlSyncProvider localProvider = new SqlSyncProvider("filtered_customer", clientConnection);
SqlSyncProvider remoteProvider = new SqlSyncProvider("filtered_customer", serverConnection);

// 定義資料同步協調者
Microsoft.Synchronization.SyncOrchestrator syncOrchestrator = new Microsoft.Synchronization.SyncOrchestrator();
syncOrchestrator.Direction = Microsoft.Synchronization.SyncDirectionOrder.Download; //指定只由Sever下載更新,不上傳。
syncOrchestrator.LocalProvider = localProvider;
syncOrchestrator.RemoteProvider = remoteProvider;

// 進行同步
Microsoft.Synchronization.SyncOperationStatistics syncStats;
syncStats = syncOrchestrator.Synchronize();

strMsg = "同步開始時間:";
strMsg += syncStats.SyncStartTime.ToLongTimeString().Trim();
strMsg += "\n\r";
strMsg += "同步結束時間:";
strMsg += syncStats.SyncEndTime.ToLongTimeString().Trim();
strMsg += "\n\r";
strMsg += "同步資料總筆數:";
strMsg += syncStats.UploadChangesApplied.ToString().Trim();

MessageBox.Show(strMsg);
}

/*----- 佈建Server端以及Client端必要的table & stored procedure & trigger,僅有第一次建置時需要執行-----*/
private void btnBuild_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection clientConnection = new System.Data.SqlClient.SqlConnection("Server=localhost\\SQLEXPRESS;Initial Catalog=pos;Persist Security Info=True;User ID=YOURID;Password=YOURPWD");
System.Data.SqlClient.SqlConnection serverConnection = new System.Data.SqlClient.SqlConnection("Server=SQLSERVER;Initial Catalog=pos;Persist Security Info=True;User ID=YOURID;Password=YOURPWD");

// 定義同步範圍與資料表
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("filtered_customer");
DbSyncTableDescription customerDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("customer", serverConnection);
scopeDesc.Tables.Add(customerDesc);
SqlSyncScopeProvisioning serverConfig = new SqlSyncScopeProvisioning(scopeDesc);
serverConfig.ObjectSchema = "customer";
serverConfig.SetCreateTableDefault(DbSyncCreationOption.Skip);

// 將設定套用至server & client,由MSF自動佈建所需的table、trigger、SP
serverConfig.Apply(serverConnection);
serverConfig.Apply(clientConnection);

MessageBox.Show("同步環境建置完成");
}
}
}

沒有留言:

張貼留言