Extract & Save SQL Statements of Execute SQL Tasks within a Sequence Container using C#

By | May 4, 2011

Abstract

This article explains how to extract and save SQL Statements found in Execute SQL Tasks of an SSIS package that contains Sequence container using C# programming language.

Requirements

Article

This article continues from another article which can be found here.

Launch Visual Studio 2008 and create an Integration Services Project. After the default (new) package has launched, drag a script task to the control flow pane.

Right click to edit the script task. In your Script Task Editor ensure that you have selected Microsoft Visual C# as your programming language.

At the bottom of your Script Task Editor, click “Edit Script”.

Add the following references:

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;

Proceed to load the package (that contains the Execute SQL Tasks you would like to extract and save).
Application app = new Application();

Load package
Package p = app.LoadPackage(“C:\\TEMP\\pkg_Execute_Sql_Tasks.dtsx”, null);

Establish a connection to SQL database where you will store the extracted SQL statements:
SqlConnection connectiont = new SqlConnection(
string.Format(“Data Source={0};Initial Catalog={1};Integrated Security=SSPI;”, “(local)”, “your_db”));
SqlCommand commandt;
connectiont.Open();

Declare variables that will be used to store extracted queries:
string src_query2 = “”;
string src_query3 = “”;
string src_query = “”;
string sql_task_name = “”;

The rest of the code is as follows:
foreach (Executable executable in importPackage.Executables)
{
//This part is for Execute SQL Tasks found within the Sequence Container
DtsContainer container = (DtsContainer)executable;
if (executable.GetType().Name == “Sequence”)
{
Sequence seq = (Sequence)executable;
foreach (Executable Seq_executable in seq.Executables)
{
DtsContainer Seq_container = (DtsContainer)Seq_executable;
if (Seq_executable.GetType().Name == “TaskHost”)
{
TaskHost loop = (TaskHost)Seq_executable;
ExecuteSQLTask sqlTask = (ExecuteSQLTask)loop.InnerObject;
src_query2 = sqlTask.SqlStatementSource;
src_query3 = src_query2.ToUpper();
src_query = src_query3;
sql_task_name = Seq_container.Name;

//Split extracted queries on keyword Go
string source3 = src_query;
string[] stringSeparators3 = new string[] { “GO” };
string[] result3;

result3 = source3.Split(stringSeparators3, StringSplitOptions.None);

foreach (string s in result3)
{
//NB: You can replace table name “SQL_Task” with your own table
commandt = new SqlCommand(“INSERT INTO SQL_Task VALUES(@SRC_Q,@SQL_NAME)”, connectiont);
commandt.Parameters.Add(new SqlParameter(“@SRC_Q”, s));
commandt.Parameters.Add(new SqlParameter(“@SQL_NAME”, sql_task_name));
commandt.ExecuteNonQuery();
}
}
}
}
//This part is for Execute SQL Tasks found outside of the Sequence Container
if (executable.GetType().Name == “TaskHost”)
{
TaskHost loop = (TaskHost)executable;
ExecuteSQLTask sqlTask = (ExecuteSQLTask)loop.InnerObject;
src_query = sqlTask.SqlStatementSource;
sql_task_name = container.Name;

string source = src_query;
string[] stringSeparators = new string[] { “GO” };
string[] result;

result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
commandt = new SqlCommand(“INSERT INTO SQL_Task VALUES(@SRC_Q,@SQL_NAME)”, connectiont);
commandt.Parameters.Add(new SqlParameter(“@SRC_Q”, s));
commandt.Parameters.Add(new SqlParameter(“@SQL_NAME”, sql_task_name));
commandt.ExecuteNonQuery();
}
}
}

We then save the package in a file system.

SIFISO_app.SaveToXml(“C:\\TEMP\\pkg_Execute_Sql_Tasks.dtsx”, dyna_pkg, null);

Conclusion

It’s that simple!

You can now execute your script task and the package will be created in location you specified.

12 thoughts on “Extract & Save SQL Statements of Execute SQL Tasks within a Sequence Container using C#

  1. save the environment

    My brother recommended I would possibly like this blog. He was totally right. This post truly made my day. You can not believe simply how much time I had spent for this info! Thank you!

    Reply
  2. Rey Barros

    Fantastic items from you, man. I’ve have in mind your stuff previous to and you are just too excellent. I actually like what you’ve acquired here, really like what you are saying and the way wherein you assert it. You’re making it enjoyable and you still care for to stay it wise. I can not wait to read far more from you. That is actually a tremendous site.

    Reply
  3. zynga chips

    wonderful submit, very informative. I’m wondering why the other experts of this sector don’t realize this. You must proceed your writing. I am confident, you’ve a huge readers’ base already!

    Reply
  4. Millie

    Good information! I have been looking for everything similar to this for a time currently. Regards!

    Reply

Leave a Reply