Search code examples
databaseazureazure-sql-databasemicrosoft-sync-frameworkazure-data-sync

Entire local database sync with sql azure


I am developing a windows application which is using local database. I want to add a function to sync all local data to the sql azure.

Currently I used following code. It enabled me to sync one particular table successfully., here is "Author_Master"

 string sqlazureConnectionString = "XXXX";
 string sqllocalConnectionString = "Server=localhost;Database=Enh_Branchwise_Master_Bookshop;Trusted_Connection=True";

        using (SqlConnection serverCon = new SqlConnection(sqlazureConnectionString))
        using (SqlConnection clientCon = new SqlConnection(sqllocalConnectionString))
        {
            var provider1 = new SqlSyncProvider("scope1", serverCon);
            var provider2 = new SqlSyncProvider("scope1", clientCon);

            prepareServer(provider1);
            prepareClinet(provider2, serverCon);
            SyncOrchestrator sync = new SyncOrchestrator();
            sync.LocalProvider = provider1;
            sync.RemoteProvider = provider2;

            sync.Synchronize();

        }

And following methods also.

 private static void prepareServer(SqlSyncProvider provider)
    {
        SqlConnection connection = (SqlConnection)provider.Connection;
        SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);

        if (!config.ScopeExists(provider.ScopeName))
        {
            DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
            scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Author_Master", connection));
            config.PopulateFromScopeDescription(scopeDesc);
            config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
            config.Apply();
        }
    }

    private static void prepareClinet(SqlSyncProvider provider, SqlConnection sourceConnection)
    {
        SqlConnection connection = (SqlConnection)provider.Connection;
        SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);

        if (!config.ScopeExists(provider.ScopeName))
        {
            DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
            scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Author_Master", sourceConnection));
            config.PopulateFromScopeDescription(scopeDesc);
            config.Apply();
        }
    }

My question : Is there any way to sync all the tables in the database at once, without adding one by one table .

My both Databases have the same schema. Please give some suggestions,


Solution

  •   Dim clientProvision As SqlSyncScopeProvisioning = New SqlSyncScopeProvisioning(clientCon, syncScope)
    
    
                                If Not (clientProvision.ScopeExists("Scope_" + tableName)) Then
                                    clientProvision.Apply()
                                End If
    
                                Dim serverProvision As SqlSyncScopeProvisioning = New SqlSyncScopeProvisioning(serverCon, syncScope)
    
                                If Not (serverProvision.ScopeExists("Scope_" + tableName)) Then
                                    serverProvision.Apply()
                                End If
    
                                Dim syncOrchestrator As New SyncOrchestrator()
    
                                ' Create provider for SQL Server
                                Dim clientProvider As New SqlSyncProvider("Scope_" + tableName, clientCon)
    
                                ' Set the command timeout and maximum transaction size for the SQL Azure provider.
                                Dim serverProvider As New SqlSyncProvider("Scope_" + tableName, serverCon)
    
                                ' Set Local provider of SyncOrchestrator to the onPremise provider
                                syncOrchestrator.LocalProvider = clientProvider
    
                                ' Set Remote provider of SyncOrchestrator to the azureProvider provider 
                                syncOrchestrator.RemoteProvider = serverProvider
    
                                ' Set the direction of SyncOrchestrator session to Upload and Download
                                syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload
    
    
                                'Dim thread As New Threading.Thread(Sub() ShowStatistics(syncOrchestrator.Synchronize(), tableName))
                                'thread.Start()
                                ShowStatistics(syncOrchestrator.Synchronize(), tableName)