2
votes

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,

4
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not".John Saunders
Dear i am searching for the same how u have done this for one table can u please help me this thing for meNeeraj Mehta
I have used following code to do that. You can try.las
@las in m doing the same thing in mvc4 i have used your code but showing error in SqlSyncProvider and allNeeraj Mehta
@las now i have remove all the code but now the problem is that it is not executingNeeraj Mehta

4 Answers

2
votes

You can use SQL Data Sync Tool.

SQL Data Sync is a feature of Windows Azure SQL Database.

With SQL Data Sync you can synchronize selected data through a Windows Azure SQL Database instance.

SQL Data Sync supports synchronizations within or across Windows Azure data centers.

SQL Data Sync also supports hybrid configurations of SQL Database instances and on-premises SQL Server databases.

The SQL Data Sync service is free.

For more details check ScottGu's Blog Post under "SQL Data Sync" sub topic.

I hope this will help to you.

1
votes

if you're using Sync Framework, you have to tell it explicitly which tables (or even columns) to actually sync.

0
votes
  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)
-1
votes

You can use SQL Database Migration Wizard (SQLAzureMW) for that.

It is an open source application.

You can use it to migrate your SQL database to and from Windows Azure SQL Database.

SQLAzureMW has a user interactive wizard that walks a person through the analysis / migration process.

For get more details about SQL Database Migration Wizard

I hope this will help to you.