Copy data of each table from server A to server B dynamically using SSIS

巧了我就是萌 提交于 2019-12-05 18:53:42

You can use C# SMO objects from a Script Task to do the transfer for a dynamic table list. The SSIS loop won't be necessary. The SSIS object variable (GlobalListOfTables) will need to be included in the ReadOnlyVariables field on the Script Task. Make sure to add Microsoft.SqlServer.SmoExtended and Microsoft.SqlServer.ConnectionInfo references to the Script Task in addition to those listed below.

using System.Data;
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Generic;
using System.Data.OleDb;



       string databaseName = "DatabaseName";
       List<string> tableNames = new List<string>();
       DataTable dt = new DataTable();
       OleDbDataAdapter dataAdapter = new OleDbDataAdapter();

        //get table names from SSIS object variable
       dataAdapter.Fill(dt, Dts.Variables["User::SourceServerName"].Value);

        //populate list
        foreach (DataRow dr in dt.Rows)
        {
          tableNames.Add(dr[0].ToString());
        }

        //create source server object
        Server srcServ = new Server(@"SourceServerName");
        srcServ.ConnectionContext.LoginSecure = true;
        srcServ.ConnectionContext.StatementTimeout = 600;
        srcServ.ConnectionContext.Connect();

        //define source database as smo object
        Database sourceDatabase = srcServ.Databases["SourceDatabaseName"];

        Transfer transfer = new Transfer();
        transfer.Database = sourceDatabase;

        //set destination server and database
        transfer.DestinationServer = @"DestinationServerName";
        transfer.DestinationDatabase = databaseName;

        //overwrite objects if they exist
        transfer.DropDestinationObjectsFirst = true;

        transfer.CopyAllObjects = false;
        transfer.CopySchema = true;

        //include data
        transfer.CopyData = true;

        foreach (Table t in sourceDatabase.Tables)
        {   
            //extract table names that were originally in SSIS object variable and avoid system objects
            if (tableNames.Contains(t.Name) && !t.IsSystemObject)
            {
                transfer.ObjectList.Add(t);
            }
        }
        //transfer objects
         transfer.TransferData();

Transfer Database Task and Transfer SQL Server Objects Task

If you are looking to copy all data from a database to another you should refer to the Transfer Database Task and Transfer SQL Server Objects Task:

Based on the Transfer Database Task official documentation:

The Transfer Database task transfers a SQL Server database between two instances of SQL Server. In contrast to the other tasks that only transfer SQL Server objects by copying them, the Transfer Database task can either copy or move a database. This task can also be used to copy a database within the same server.

Also, based on Transfer SQL Server Objects Task official documentation:

The Transfer SQL Server Objects task transfers one or more types of objects in a SQL Server database between instances of SQL Server. For example, the task can copy tables and stored procedures. Depending on the version of SQL Server that is used as a source, different types of objects are available to copy. For example, only a SQL Server database includes schemas and user-defined aggregates.

Detailed articles

There are many article describing how to work with these tasks and all available options, as example:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!