0

我是 Microsoft Sync Framework 的新手,我正在使用 Microsoft 的以下示例进行测试(请参阅http://msdn.microsoft.com/en-us/library/ff928758.aspx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;

namespace ExecuteExpressSync
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection clientConn = new SqlConnection(@"Data Source=.\SQLCLIENT; Initial Catalog=SyncExpressDB; Trusted_Connection=Yes");

            SqlConnection serverConn = new SqlConnection("Data Source=localhost\\SQLEXPRESS; Initial Catalog=SyncDB; Integrated Security=True");

            // create the sync orhcestrator
            SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

            // set local provider of orchestrator to a sync provider associated with the 
            // ProductsScope in the SyncExpressDB express client database
            syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);


            // set the remote provider of orchestrator to a server sync provider associated with
            // the ProductsScope in the SyncDB server database
            syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);


            // 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
            ((SqlCeSyncProvider)syncOrchestrator.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
            ((SqlCeSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

            // 
            makeConflict(clientConn, "999");
            makeConflict(serverConn, "666");

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

            // print statistics
            Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
            Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
            Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
            Console.WriteLine("Download failed: " + syncStats.DownloadChangesFailed);
            Console.WriteLine("Upload Changes failed: " + syncStats.UploadChangesFailed);
            Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
            Console.WriteLine(String.Empty);

            Console.ReadLine();

        }

        static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
        {
            // display conflict type
            Console.WriteLine(e.Conflict.Type);

            // display error message 
            Console.WriteLine(e.Error);
        }

        private static void makeConflict(SqlConnection nodeConn, String price)
        {
            int rowCount = 0;

            using (nodeConn)
            {
                SqlCommand sqlCommand = nodeConn.CreateCommand();

                sqlCommand.CommandText = "UPDATE Products SET ListPrice = " + price + " WHERE Name = 'PCClient' ";


                nodeConn.Open();
                rowCount = sqlCommand.ExecuteNonQuery();
                nodeConn.Close();

            }
        }

    }
}

我的本地 PC 上有两个 SQL-Server 实例(SQLCLIENT 和 SQLEXPRESS)用于测试环境。我的问题是,尽管我通过调用 makeConflict() 来创建冲突,但 ApplyChangeFailed-Event 没有触发,该方法对产品表中的一行执行一次更新,服务器和客户端一次。结果是服务器或客户端获胜,具体取决于 SyncDirectionOrder 属性。

我做错了什么?

4

3 回答 3

1

改变这个:

((SqlCeSyncProvider)syncOrchestrator

对此:

((SqlSyncProvider)syncOrchestrator

您使用的是 SqlSyncProvider,而不是 SqlCeSyncProvider

于 2013-06-25T06:39:08.457 回答
0

有用!我猜我需要一台专用服务器是正确的。我在两个客户端主机上更改了同一行,并且在同步第二个客户端后,“localupdateremoteupdate”冲突已被触发。

于 2013-06-27T07:14:00.910 回答
0

为避免进一步混淆,这里是实际代码。问题仍然存在。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using Microsoft.Synchronization.Data.SqlServerCe;

namespace ExecuteExpressSync
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection clientConn = new SqlConnection(@"Data Source=.\SQLCLIENT; Initial Catalog=SyncExpressDB; Trusted_Connection=Yes");

            SqlConnection serverConn = new SqlConnection("Data Source=localhost\\SQLEXPRESS; Initial Catalog=SyncDB; Integrated Security=True");

            // create the sync orhcestrator
            SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

            // set local provider of orchestrator to a sync provider associated with the 
            // ProductsScope in the SyncExpressDB express client database
            syncOrchestrator.LocalProvider = new SqlSyncProvider("ProductsScope", clientConn);


            // set the remote provider of orchestrator to a server sync provider associated with
            // the ProductsScope in the SyncDB server database
            syncOrchestrator.RemoteProvider = new SqlSyncProvider("ProductsScope", serverConn);


            // 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.RemoteProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
            ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

            // 
            makeConflict(clientConn, "999");
            makeConflict(serverConn, "666");

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

            // print statistics
            Console.WriteLine("Start Time: " + syncStats.SyncStartTime);
            Console.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
            Console.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
            Console.WriteLine("Download failed: " + syncStats.DownloadChangesFailed);
            Console.WriteLine("Upload Changes failed: " + syncStats.UploadChangesFailed);
            Console.WriteLine("Complete Time: " + syncStats.SyncEndTime);
            Console.WriteLine(String.Empty);

            Console.ReadLine();

        }

        static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
        {
            // display conflict type
            Console.WriteLine(e.Conflict.Type);

            // display error message 
            Console.WriteLine(e.Error);
        }

        private static void makeConflict(SqlConnection nodeConn, String price)
        {
            int rowCount = 0;

            using (nodeConn)
            {
                SqlCommand sqlCommand = nodeConn.CreateCommand();

                sqlCommand.CommandText = "UPDATE Products SET ListPrice = " + price + " WHERE Name = 'PCClient' ";


                nodeConn.Open();
                rowCount = sqlCommand.ExecuteNonQuery();
                nodeConn.Close();

            }
        }

    }
}
于 2013-06-25T11:30:09.623 回答