跟踪Microsoft Sync Framework中的更改

时间:2016-09-06 20:58:21

标签: c# sql .net microsoft-sync-framework

下面是我的代码,它执行两个2008 SQL Server之间的同步。该程序成功地同步两台服务器之间的数据。然而问题是,在我生成的日志中,我注意到从Server2到Server1的大量事务 - 总是在这个方向上,并且总是非常相似的数量。为了帮助追踪发生这种情况的原因,我想创建另一个日志文件,该文件记录每次脚本同步两台服务器时从一台服务器复制到另一台服务器的实际行数据。有没有办法使用Sync Framework执行此操作,还是更好地利用SQL Server中的其他实用程序来执行此操作?我对数据库并不十分精通,所以最基本,最直接的解决方案对我来说是最理想的。

//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 个答案:

答案 0 :(得分:1)

如果您为ChangesSelected或ChangesApplied创建处理程序,那么其中的数据集将包含已选择或应用的实际数据。