2016-09-06 110 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