none
Microsoft Sync Framework RRS feed

  • Question

  • I am using Microsoft Sync Framework for syncing server and client SQL server 2008 database. My requirement is to get summary of all changes and display it to the user before actually perform sync operation.

    Does any one have idea how can we get changes in microsoft sync framework before actually synchronize them?

    Monday, July 4, 2011 7:07 AM

Answers

  • this one works for me:

     // create the sync orchestrator
          var syncOrchestrator = new SyncOrchestrator();
    
          //setup providers
          var localProvider = new SqlSyncProvider("TestScope", clientConn);
          var remoteProvider = new SqlSyncProvider("TestScope", serverConn);
          
          //set the providers
          syncOrchestrator.LocalProvider = localProvider;
          syncOrchestrator.RemoteProvider = remoteProvider;
    
          // subscribe to changes selected event
          localProvider.ChangesSelected += localProvider_ChangesSelected;
          remoteProvider.ChangesSelected += remoteProvider_ChangesSelected;
    
          // set the direction of sync session to Upload
          syncOrchestrator.Direction = SyncDirectionOrder.Upload;
    
          // execute the synchronization process
          var syncStats = syncOrchestrator.Synchronize();
    
          // set the direction of sync session to Download
          syncOrchestrator.Direction = SyncDirectionOrder.Download;
    
          // execute the synchronization process
          var syncStats2 = syncOrchestrator.Synchronize();
    

    Monday, July 4, 2011 2:15 PM
    Moderator

All replies

  • please dont keep on opening up new threads, just modify your other thread.

    to answer your question, subscribe to the ChangesSelected Event on both local and remote provider, do an upload sync to get the local changes, once you get the changes in the ChangesSelected event, abort the sync by throwing an exception.  then do the same again by changing the sync direction to download so you get the changes from the remote provider.

    Monday, July 4, 2011 7:14 AM
    Moderator
  • Thanks a lot for giving the reply.If possible could you please send code snippet for this.

     

    How to subscribe to the ChangesSelected Event on both local and remote provider?

    Monday, July 4, 2011 7:29 AM
  • Monday, July 4, 2011 7:52 AM
    Moderator
  • i have written the code i.e

          SyncOrchestrator oOrchestrator = new SyncOrchestrator
                        {
                            LocalProvider = new SqlSyncProvider("oSyncScope", oClientConn),
                            RemoteProvider = new SqlSyncProvider("oSyncScope", oServConn)
                            //Direction = SyncDirectionOrder.Upload
                           

                        };

                        ((SqlSyncProvider)oOrchestrator.LocalProvider).ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(ClientChangesSelected);
                        dataGridView1.DataSource = dtClient;
                        oOrchestrator.Direction = SyncDirectionOrder.Upload;
                        ((SqlSyncProvider)oOrchestrator.RemoteProvider).ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(ServerChangesSelected);
                        dataGridView1.DataSource = dtServer;
                        oOrchestrator.Direction = SyncDirectionOrder.Download;
                        SyncOperationStatistics syncStats = oOrchestrator.Synchronize();

     

    But i am unable to get the changes.Could u please help me.

     

    Monday, July 4, 2011 9:47 AM
  • This is continuous code to above;

     

     static void ServerChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {

                if (e.Context.DataSet.Tables.Contains("emp"))
                {
                   
                DataTable  dtServer = e.Context.DataSet.Tables["emp"].GetChanges();
                
                   
                }
            }
            static void ClientChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {
                if (e.Context.DataSet.Tables.Contains("emp"))
                {

                   DataTable dtClient = e.Context.DataSet.Tables["emp"].GetChanges();

                }
            }

    Monday, July 4, 2011 9:49 AM
  • you have to do one synchronize session each for Upload and Download. you also need to throw an exception in the ChangesSelected event to cancel the sync.

    e.g.,

     

    SyncOrchestrator oOrchestrator = new SyncOrchestrator
    {
    LocalProvider = new SqlSyncProvider("oSyncScope", oClientConn),
    RemoteProvider = new SqlSyncProvider("oSyncScope", oServConn)
    //Direction = SyncDirectionOrder.Upload


    };

    ((SqlSyncProvider)oOrchestrator.LocalProvider).ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(ClientChangesSelected);
    dataGridView1.DataSource = dtClient;
    ((SqlSyncProvider)oOrchestrator.RemoteProvider).ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(ServerChangesSelected);
    dataGridView1.DataSource = dtServer;

    //get changes for upload

    oOrchestrator.Direction = SyncDirectionOrder.Upload;
    SyncOperationStatistics syncStats = oOrchestrator.Synchronize();

    //get changes for download

    oOrchestrator.Direction = SyncDirectionOrder.Download;
    SyncOperationStatistics syncStats = oOrchestrator.Synchronize();

    Monday, July 4, 2011 10:08 AM
    Moderator
  • Thanks June,But i need to get changes and should display changes in datagridviews(client and server).

    Could u please help me.
    Monday, July 4, 2011 10:29 AM
  • once you get the changes from the ChangesSelected event, you can bind it to your datagrid.
    Monday, July 4, 2011 10:34 AM
    Moderator
  • But i am unable to getting the changes
    Monday, July 4, 2011 10:37 AM
  • you mean the ChangesSelected is not firing? have you made updates to either your local or remote database?
    Monday, July 4, 2011 10:48 AM
    Moderator
  • Yes, i made changes in local database,but not getting changes

     

    this is the code i have written,is there any wrong.

     

     static void ServerChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {

                if (e.Context.DataSet.Tables.Contains("emp"))
                {
                   
                DataTable  dtServer = e.Context.DataSet.Tables["emp"].GetChanges();
                
                   
                }
            }
            static void ClientChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {
                if (e.Context.DataSet.Tables.Contains("emp"))
                {

                   DataTable dtClient = e.Context.DataSet.Tables["emp"].GetChanges();

                }
            }

    Monday, July 4, 2011 10:51 AM
  • you dont have to call GetChanges on the Datatable.

    Table dtClient = e.Context.DataSet.Tables["emp"];

     

    is the ChangesSelected firing at all? or youre not simply getting anything from the Context?

    Monday, July 4, 2011 1:07 PM
    Moderator
  • ChangesSelected event is not firing,so what to do
    Monday, July 4, 2011 1:26 PM
  • while debugging mode cursor is not entering in side the below code snippet.

    static void ServerChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {

                if (e.Context.DataSet.Tables.Contains("emp"))
                {
                   
                DataTable  dtServer = e.Context.DataSet.Tables["emp"];
                
                   
                }
            }

    Monday, July 4, 2011 1:39 PM
  • if you updated the local database, its the ClientChangesSelected that should be firing.

    the ServerChangesSelected  should be firing when there is an update on the server side.

    Monday, July 4, 2011 1:48 PM
    Moderator
  • even ClientChangesSelectedis not firing.i added breakpoint over there,its not going inside.Could u please help me

    ((SqlSyncProvider)oOrchestrator.LocalProvider).ChangesSelected += new EventHandler<DbChangesSelectedEventArgs>(ClientChangesSelected);
                        dataGridView1.DataSource = dtClient ;

    static void ClientChangesSelected(object sender, DbChangesSelectedEventArgs e)
            {
                if (e.Context.DataSet.Tables.Contains("emp"))
                {

                   DataTable dtClient = e.Context.DataSet.Tables["emp"];

                }
            }

    Monday, July 4, 2011 2:01 PM
  • this one works for me:

     // create the sync orchestrator
          var syncOrchestrator = new SyncOrchestrator();
    
          //setup providers
          var localProvider = new SqlSyncProvider("TestScope", clientConn);
          var remoteProvider = new SqlSyncProvider("TestScope", serverConn);
          
          //set the providers
          syncOrchestrator.LocalProvider = localProvider;
          syncOrchestrator.RemoteProvider = remoteProvider;
    
          // subscribe to changes selected event
          localProvider.ChangesSelected += localProvider_ChangesSelected;
          remoteProvider.ChangesSelected += remoteProvider_ChangesSelected;
    
          // set the direction of sync session to Upload
          syncOrchestrator.Direction = SyncDirectionOrder.Upload;
    
          // execute the synchronization process
          var syncStats = syncOrchestrator.Synchronize();
    
          // set the direction of sync session to Download
          syncOrchestrator.Direction = SyncDirectionOrder.Download;
    
          // execute the synchronization process
          var syncStats2 = syncOrchestrator.Synchronize();
    

    Monday, July 4, 2011 2:15 PM
    Moderator
  • Ok,fine.But my requirement is get only changes on both localprovider and remoteprovider,and should display to the user in datagridviews.

     

    Could u please give me code for this.

    Monday, July 4, 2011 2:24 PM
  • The same code i wrote,which u send to me,chages are effecting,but i need to pickup changes 
    Monday, July 4, 2011 2:26 PM
  • so are you getting the ChangesSelected event already?
    Tuesday, July 5, 2011 1:10 AM
    Moderator
  • yes,but i want to display changes in datagridview,can u please send code for this.
    Tuesday, July 5, 2011 5:28 AM
  • Hello Jagadeesh - JuneT has given you lots of pointers about how to do this, but you will have look up the documentation and samples and write the code yourself. It is not possible to share out code and walkthroughs for this on the forum. Please install the SDK and learn about the providers and then it will be clearer on how to do this yourself.

     

    thanks

    Tuesday, July 19, 2011 9:55 PM
    Moderator
  • Complete solution build on ntier Sync : http://www.rajneeshnoonia.com/blog/2012/03/n-tier-sync-framework/

    Regards

    Rajnish

    Tuesday, June 12, 2012 4:25 PM