iT邦幫忙

12

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

Microsoft發行的Microsoft Sync Framework,是一個全面性的同步平台,提供資料庫、檔案、Web摘要的資料同步所需的framework。
Microsoft發行的Microsoft Sync Framework,是一個全面性的同步平台,提供資料庫、檔案、Web摘要的資料同步所需的framework。
開發人員得以這個平台,快速開發啟用漫遊、資料共用及離線處理資料的應用程式。

以下的範例,是一個同步SQL Server 2008 Express & SQL Server 2008 Enterprise的簡易範例。希望與各位分享這個小小的心得。

此範例以Visual C# 2008 + MSF 2.0設計。Form上面只有兩個按鈕,btnBuild,用來建置環境,btnSync用來執行同步。
在此之前,必須於client與server端建立一個pos資料庫,並建立customer資料表,該資料表,必須定義一個primary key,作為同步識別之用。

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("同步環境建置完成");
    }
  }
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
Ken(Bigcandy)
iT邦大師 1 級 ‧ 2009-12-18 10:04:54

雖然看不懂,還是推~~

外獅佬 iT邦大師 1 級 ‧ 2009-12-18 11:32:07 檢舉

MSF是一個基於.net的資料同步平台,這個一個簡單的資料庫同步的sample,應用還有很多,甚至可以應用在異質資料庫的同步,還在研究當中~~

0
jackhsuster
iT邦新手 5 級 ‧ 2010-06-20 21:09:16

請問一下這跟資料庫複寫的功能有什麼差別 ?

外獅佬 iT邦大師 1 級 ‧ 2010-06-20 22:46:36 檢舉

MSF只要是支援ADO.net的資料庫,都可以做到同步,甚至異質資料庫同步。

0
huchungling
iT邦新手 2 級 ‧ 2012-11-15 14:03:53

wiselou提到:
異質資料庫同步

異質資料庫同步 這到不錯.

0
t0989959118
iT邦新手 5 級 ‧ 2013-08-16 22:50:34

如果是 代管中心 是否能跟本機同步

外獅佬 iT邦大師 1 級 ‧ 2013-08-17 01:11:36 檢舉

代管中心?可以,但是,必須要有存取資料庫的權限,sync framework,
需要有建立資料表的權限,因為他會建立資料表以及trigger,記錄資料的新增、刪除、修改記錄
而且,最好把同步的程式放在本機執行
為什麼?因為代管中心,你可能連安裝程式的機會都沒有

0
zonghanch
iT邦新手 5 級 ‧ 2013-12-25 15:41:54

你好, 如果已經使用 DbSyncScopeDescription 佈建Server端以及Client端必要的table,但是後來 table 中有新增其他欄位,發現同步化時沒有同步化到新增的欄位內容,重新要使用 DbSyncScopeDescription 佈建Server端以及Client端必要的table會發生錯誤

請問有辦法將布建完成的設定重置或是更新嗎?

我要留言

立即登入留言