简体   繁体   中英

Copy data from on-premises SQL Server database to Azure SQL Server database

I have two SQL Server instances, one being an on-premise SQL Server and the other being Azure SQL Server instance. Some of the tables in the Azure SQL Server database have a few columns which contain data from the on-premise SQL Server database (although the table schemas are different).

We need to make sure that whenever new entries are added into the on-premises SQL Server database, the corresponding entries should get inserted in the Azure SQL Server database as well.

What is the best way to do this?

You can create your own code using Sync Framework to specify what specific tables you want sync.

using (SqlConnection sqlServerConn =
    new SqlConnection(LocalSQLServerConnectionString))
{
    using (SqlConnection sqlAzureConn =
        new SqlConnection(RemoteSQLAzureConnectionString))
    {
        DbSyncScopeDescription myScope =
            new DbSyncScopeDescription(scopeName);
        DbSyncTableDescription Customer =
            SqlSyncDescriptionBuilder.GetDescriptionForTable("SalesLT.Customer", sqlServerConn);

        DbSyncTableDescription Product =

            SqlSyncDescriptionBuilder.GetDescriptionForTable("SalesLT.Product", sqlServerConn);

        // Add the tables from above to the scope
        myScope.Tables.Add(Customer);
        myScope.Tables.Add(Product);

The next section of code sets up the local on-premise SQL Server for provisioning. If the SQL Server already contains the table schemas and data then what does it have to do? The Synchronization Framework uses both databases as data storage to store configuration information, and state information about the current status of the synchronization. So the provisioning creates tables on your local SQL Server to store this information.

// Setup SQL Server for sync

SqlSyncScopeProvisioning sqlServerProv =
    new SqlSyncScopeProvisioning(sqlServerConn, myScope);
if (!sqlServerProv.ScopeExists(scopeName))

    // Apply the scope provisioning.
    sqlServerProv.Apply();

The next section of code does the same thing for the remote SQL Database server. However, it also creates the schemas data tables that it is going to synchronize too, based on the local SQL Server scope. Here is what the code looks like:

// Setup SQL Database for sync

SqlSyncScopeProvisioning sqlAzureProv =
    new SqlSyncScopeProvisioning(sqlAzureConn, myScope);
if (!sqlAzureProv.ScopeExists(scopeName))

    // Apply the scope provisioning.
    sqlAzureProv.Apply();

To synchronize the databases just run the console application like this:

SyncConsole.exe –setup

Database setup just needs to happen once, however you will might want to synchronize the database multiple, because of this the code is split into two different sections one for setup and one for synchronization.

The code synchronizing the data is just as simple. Here is what it looks like:

using (SqlConnection sqlServerConn = new SqlConnection(LocalSQLServerConnectionString))

{
    using (SqlConnection sqlAzureConn = new SqlConnection(RemoteSQLAzureConnectionString))
    {
        SyncOrchestrator syncOrchestrator = new SyncOrchestrator
        {
            LocalProvider = new SqlSyncProvider(scopeName, sqlAzureConn),
            RemoteProvider = new SqlSyncProvider(scopeName, sqlServerConn),
            Direction = SyncDirectionOrder.UploadAndDownload
        };
         syncOrchestrator.Synchronize();
    }
}

In the synchronization code we create two connection and instantiate a sync orchestrator, telling it that we want to upload and download the data. This is considered bi-directional synchronization, writes in either SQL Database or SQL Server to be moved to the other.

To synchronize the databases just run the console application like this:

SyncConsole.exe –sync

Once the synchronization has completed, we can query the SQL Database and see that the data in is there.

To see a full example of how to do it, please visit this article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM