Abstract
This article explains how to Programmatically create an SQL Server Integration Services (SSIS) package with a Transfer SQL Server Objects component. The Transfer SQL Server Objects tasks allow ETL developers to copy one or more Microsoft SQL server objects (i.e. views, stored procedures, tables, schema, etc) into another SQL instance. For the purposes of this research, the article will explain how to transfer a SQL Server database Schema from one instance into another.
Requirements
- Microsoft Visual Studio 2008 (or later editions)
- SQL Server 2005 (or later editions)
- AdventureWorks2008 database (downloadable database file available here)
Article
If the above requirements are all met, we will begin by launching the Microsoft Visual Studio edition.
Create a new project Integration Services Project which is located under Business Intelligence Projects.
After you have named the new project, proceed to click and drag the script task in the Control Flow pane of the new package.
Right-click the script task and click on “Edit”
Under the Script Task Editor change the “ScriptLanguage” to “Microsoft Visual C# 2008”.
In Project Explorer import relevant references and ensure that you have declared namespaces as below:
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Dts.Tasks;
using Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using RuntimeWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
After declarations, create an instance of the application and package:
Application SIFISO_app = new Application();
Package p = new Package();
Declare an SMO server connection for both the source and destination server:
ConnectionManager src_cm = p.Connections.Add(“SMOServer”);
src_cm.Name = “source_conn”;
src_cm.ConnectionString = string.Format(“SqlServerName={0}; UseWindowsAuthentication={1};”, “your_source_server”, “True”);
ConnectionManager des_cm = p.Connections.Add(“SMOServer”);
des_cm.Name = “des_conn”;
des_cm.ConnectionString = string.Format(“SqlServerName={0}; UseWindowsAuthentication={1};”, “your_destination_server”, “True”);
Declare a variable that will be used to capture schema names to be transferred and assign values to the newly declared variable:
string Schema_list_names = “[dbo].[sp_vTimeSeries]”;
StringCollection tb_Col = new StringCollection();
String[] tb_list = null;
Insert the Transfer SQL Server Object component into the package and assign the name and description of the component:
Executable exec = p.Executables.Add(“STOCK:TransferSQLServerObjectsTask”);
TaskHost th = exec as TaskHost;
th.Properties[“Name”].SetValue(th, “Transfer SQL Server Objects Task – Schema”);
th.Properties[“Description”].SetValue(th, “Transfer SQL Server Objects Task – Schema”);
Supply the source and destination SQL Server databases:
string SourceDatabase = “AdventureWorksDW2008”;
string DestinationDatabase = “AdventureWorks”;
th.Properties[“SourceConnection”].SetValue(th, “source_conn”);
th.Properties[“SourceDatabase”].SetValue(th, SourceDatabase);
th.Properties[“DestinationConnection”].SetValue(th, “des_conn”);
th.Properties[“DestinationDatabase”].SetValue(th, DestinationDatabase);
Try not to remove existing Schema in destinations that are similar to the object to be copied:
Boolean DropObjectsFirst = false;
The rest of the object properties must be set as follows:
Boolean CopyAllObjects = false;
Boolean IncludeExtendedProperties = false;
Boolean CopyData = true;
Boolean CopySchema = true;
Boolean UseCollation = false;
Boolean IncludeDependentObjects = false;
th.Properties[“DropObjectsFirst”].SetValue(th, DropObjectsFirst);
th.Properties[“IncludeExtendedProperties”].SetValue(th, IncludeExtendedProperties);
th.Properties[“CopyData”].SetValue(th, CopyData);
th.Properties[“CopySchema”].SetValue(th, CopySchema);
th.Properties[“UseCollation”].SetValue(th, UseCollation);
th.Properties[“IncludeDependentObjects”].SetValue(th, IncludeDependentObjects);
th.Properties[“CopyAllObjects”].SetValue(th, CopyAllObjects);
Boolean CopyAllSchemas = false;
Boolean Coll_CopyAllSchemas = true;
th.Properties[“CopyAllSchemas”].SetValue(th, CopyAllSchemas);
if (CopyAllSchemas == false)
{
if (Coll_CopyAllSchemas == true)
{
tb_list = new String[] { Schema_list_names };
tb_Col.AddRange(tb_list);
th.Properties[“SchemasList”].SetValue(th, tb_Col);
}
}
We then save the package into a file system.
Dts.TaskResult = (int)ScriptResults.Success;
SIFISO_app.SaveToXml(“C:\\TEMP\\pkg_Exec_Pkg_Tasks_Views.dtsx”, p, null);
Conclusion
It’s that simple!
You can now execute your script task and the package will be created in the location you specified.
0 Comments