locked
All tables sync between two database with Microsoft Framework RRS feed

  • Question

  • Dear sir/Madam

    I am trying to synch between two database for multiple table but it synch only last added scope and its related table .

    My code is-

    class Program
        {
            
            static string sServerConnection = @"Data Source=VISHAL-PC;Initial Catalog=dbServer;Integrated Security=True";//"Data Source=VISHAL-PC; Initial Catalog=dbClint; Trusted_Connection=Yes";
            static string sClientConnection = @"Data Source=VISHAL-PC; Initial Catalog=dbClint; Integrated Security=True";
            
            static string sScope0 = "NameScope";
            static string sScope1 = "test1Scope";
            static string sScope2 = "test2Scope";
            static void Main(string[] args)
            {

                ProvisionServer();
                ProvisionClient();
                //Sync(sScope0);
                for (int i = 1; i <= 3; i++)
                {
                    if (i == 1)
                    {
                        Sync(sScope0);
                        
                    }
                    else if (i == 2)
                    {
                        Sync(sScope1);
                    }
                    else if (i == 3)
                    {
                        Sync(sScope2);
                    }
                }
            }

            public static void ProvisionServer()
            {
                try {
                SqlConnection serverConn = new SqlConnection(sServerConnection);

                DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(sScope0);
                scopeDesc = new DbSyncScopeDescription(sScope1);
                scopeDesc = new DbSyncScopeDescription(sScope2);
                DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Name", serverConn);
                DbSyncTableDescription tableDesc1 = SqlSyncDescriptionBuilder.GetDescriptionForTable("test1", serverConn);
                DbSyncTableDescription tableDesc2 = SqlSyncDescriptionBuilder.GetDescriptionForTable("test2", serverConn);
                scopeDesc.Tables.Add(tableDesc);
                scopeDesc.Tables.Add(tableDesc1);
                scopeDesc.Tables.Add(tableDesc2);

                SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc);
                serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

                serverProvision.Apply();
                }
                catch (Exception ex) { }
            }

            public static void ProvisionClient()
            {
                try {
                SqlConnection serverConn = new SqlConnection(sServerConnection);
                SqlConnection clientConn = new SqlConnection(sClientConnection);

                DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(sScope0, serverConn);
                scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(sScope1, serverConn);
                scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(sScope2, serverConn);
                SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, scopeDesc);

                clientProvision.Apply();
                }
                catch (Exception ex) { }
            }

            private static void Sync(string scop_name)
            {
                SqlConnection serverConn = new SqlConnection(sServerConnection);
                SqlConnection clientConn = new SqlConnection(sClientConnection);
                
                SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

                syncOrchestrator.LocalProvider = new SqlSyncProvider(scop_name, clientConn);
                syncOrchestrator.RemoteProvider = new SqlSyncProvider(scop_name, serverConn);

              
                
                syncOrchestrator.Direction = SyncDirectionOrder.Download;

                ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
                
                SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
                

             
            }

            static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
            {            
                Console.WriteLine(e.Conflict.Type);
                Console.WriteLine(e.Error);
            }
        }

    Friday, January 8, 2016 9:30 AM