Download & Save SSIS Packages from SQL Server 2008 Instance to XML Format

By | February 10, 2012

Abstract

This article explains how to Programmatically download SQL Server Integration Services (SSIS) packages from an instance of SQL Server 2008 and save them into an XML file using a Microsoft’s SSIS Script Task component.

Requirements

Article

If the above requirements are all met, we will begin by launching 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 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 System.IO;
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.Collections.Generic;
using System.Collections.Specialized;

After declarations, create instances of application and SSIS package:

Application SIFISO_app = new Application();
Package p = new Package();

Create local variables that will be used store sql server instance name and database. (For the purposes of this discussion, the database name refers to msdb):

string set_server = “your_sql_server_instance_name”;
string config_database = “msdb”;

The below script will continue to demonstrate creating and establishing an OLE DB connection as below (please note: folderid – ‘00000000-0000-0000-0000-000000000000’ – is the default root folder):

SqlConnection connectiont = new SqlConnection(
string.Format(“Data Source={0};Initial Catalog={1};Integrated Security=SSPI;”, set_server, config_database));
SqlCommand commandt = new SqlCommand(
“select * from msdb.dbo.sysssispackages where folderid =’00000000-0000-0000-0000-000000000000′”, connectiont);
connectiont.Open();

SqlDataAdapter adap = new SqlDataAdapter(commandt);

DataSet ds = new DataSet();
adap.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
int intCount = 0;
while (intCount < (ds.Tables[0].Rows.Count)) { try { string pkg_name = ds.Tables[0].Rows[intCount].ItemArray[0].ToString(); string pkg_full_name = @"\\" + pkg_name + ""; string pkg_full_save_loc = @"C:\Sifiso\Documents\SSIS_Package_Backup\PROD\" + pkg_name + ".dtsx"; //Change package protection level to - DontSaveSensitive //This is to prevent specifying passwords for individual packages Package importPackage = SIFISO_app.LoadFromSqlServer(pkg_full_name, set_server, null, null, null); importPackage.Name = pkg_name; importPackage.ProtectionLevel = DTSProtectionLevel.DontSaveSensitive; SIFISO_app.SaveToXml(pkg_full_save_loc, importPackage, null); intCount++; } //Exception Handling //create a table that will store list of package names that failed to download catch { commandt = new SqlCommand("insert into [SIFISO_TEST].[dbo].[not_copied_tables] values('" + ds.Tables[0].Rows[intCount].ItemArray[0].ToString() + "')", connectiont); commandt.ExecuteNonQuery(); intCount++; } } }

We then save the package into a file system.

Dts.TaskResult = (int)ScriptResults.Success;
SIFISO_app.SaveToXml(“C:\\TEMP\\pkg_ArchiveToXML_Csharp.dtsx”, p, null);

Conclusion

It’s that simple!

You can now execute your script task and your packages will be saved into the file system specified.

Leave a Reply