2016-09-06 109 views
0

以下是我的兩個2008 SQL Server之間執行同步的代碼。該程序成功地在兩臺服務器之間同步數據,沒有問題。然而問題是,在這期間我產生的日誌中,我注意到從Server2到Server1的事務量非常大 - 總是在這個方向上,而且總是非常相似的數量。爲了幫助追蹤爲什麼發生這種情況,我想創建另一個日誌文件,記錄每次腳本同步兩臺服務器時從一臺服務器複製到另一臺服務器的實際行數據。有沒有辦法使用Sync Framework來做到這一點,或者使用SQL Server中的另一個實用程序來做到這一點會更好嗎?我對數據庫並不熟練,所以最基本和直接的解決方案對我來說是最理想的。跟蹤Microsoft Sync Framework中的更改

//Connection string to the client (what the data flows INTO) 
SqlConnection clientConnection = new SqlConnection("Data Source=OMITTED;Initial Catalog=OMITTED;Integrated Security=SSPI"); 

//Connection string to the database (what the data flows FROM) 
SqlConnection serverConnection = new SqlConnection("Data Source=OMITTED;Initial Catalog=OMITTED;Integrated Security=SSPI"); 

//Create a sync orchestrator 
SyncOrchestrator syncOrchestrator = new SyncOrchestrator(); 

//Set local provider of orchestrator to a sync provider (S2) 
syncOrchestrator.LocalProvider = new SqlSyncProvider("OMITTED", clientConnection); 

//Set remote provider of orchestrator to a server sync provider (S1) 
syncOrchestrator.RemoteProvider = new SqlSyncProvider("OMITTED", serverConnection); 

//Set the direction of sync session to UPload and Download 
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload; 

//Subscribe for errors that occur when applying changes to the client 
((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed); 

//Execute the synchronization process 
SyncOperationStatistics syncStats = syncOrchestrator.Synchronize(); 

//Access specific information about the given file 
FileInfo myFile = new FileInfo(LogFilePath); 

//If the log file does not yet have any data (it's blank), include some header information 
//Otherwise, just append the file with new data 
if (!(myFile.Length > 0)) 
{ 
    string header = "Run Time,Changes Uploaded,Changes Downloaded"; 
    string data = syncStats.SyncStartTime + "," + syncStats.UploadChangesTotal + "," + syncStats.DownloadChangesTotal; 

    LogFileText.Add(header); 
    LogFileText.Add(data); 
} 
else 
{ 
    string data = syncStats.SyncStartTime + "," + syncStats.UploadChangesTotal + "," + syncStats.DownloadChangesTotal; 
    LogFileText.Add(data); 
} 

回答

1

如果創建ChangesSelected或ChangesApplied的處理程序,就會有一個數據集有包含被選定或應用的實際數據。

+0

您是否在說我可以爲此創建一個自定義事件處理程序,並使用它從Sync Framework訪問某些對象信息,或者在使用僅需要添加到代碼中的Sync Framework時,這已經是可用項目了? –

+0

開箱即用。你只需要編寫事件處理程序... – JuneT