• Home
        • Database Development

          Database development is designing, creating a database or data model, and analyzing requirements and their intents as raw data.

          Learn More
        • Architecture & Design

          Software architecture refers to the fundamental structures of a software system and the discipline of creating such structures and systems.

          Learn More
        • Programming

          Computer programming is the process of performing a particular computation or more generally, accomplishing a specific result.

          Learn More
        • Cloud Computing

          Cloud computing is the on-demand availability of computer system resources, especially data storage and computing power.

          Learn More
        • ETL Development

          ETL provides the foundation for data analytics and machine learning workstreams. Through a series of business rules, ETL cleanses and organizes data.

          Learn More
        • Data Visualization & Reports

          Data and information visualization is an interdisciplinary field that deals with the graphic representation of data and information.

          Learn More
  • Blog
  • Contact

Extract SQL Statements in Execute SQL Tasks and save them into an Excel Workbook using C#

Abstract
This article explains how to extract SQL Statements found in Execute SQL Tasks of an SSIS package and save them into an Excel Workbook 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”.
The next step is to insert references.
In the Project Explorer, right-click “References” and click “Add Reference”. Click the “COM” tab and Choose Microsoft Excel 14.0 Object Library.
Add the following namespaces:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
The rest of the references should be declared as follows:
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);
Declare variables that will be used to store extracted queries:

string src_query = “”;
string sql_task_name = “”;
Int32 i = 1;

Create an instance of an excel application as follows:

Excel.Application sS_excelApp;
Excel.Workbook sS_excelWorkBook;
Excel.Worksheet sS_excelWorkSheet;
object misValue = System.Reflection.Missing.Value;

sS_excelApp = new Excel.ApplicationClass();
sS_excelWorkBook = sS_excelApp.Workbooks.Add(misValue);

sS_excelWorkSheet = (Excel.Worksheet)sS_excelWorkBook.Worksheets.get_Item(1);

// Create Headings
sS_excelWorkSheet.Cells[1, 1] = “src_query”;
sS_excelWorkSheet.Cells[1, 2] = “sql_task_name”;

The rest of the code is as follows:
foreach (Executable executable in importPackage.Executables)
{
DtsContainer container = (DtsContainer)executable;
if (executable.GetType().Name == “TaskHost”)
{
i = i + 1;
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 the result)
{
// Insert new records into the workbook
sS_excelWorkSheet.Cells[i, 1] = s;
sS_excelWorkSheet.Cells[i, 2] = sql_task_name;
}
}
}
// Save the workbook
sS_excelWorkBook.SaveAs(“C:\\TEMP\\selectSIFISO_Book.xls”, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
// Close workbook
sS_excelWorkBook.Close(true, misValue, misValue);
sS_excelApp.Quit();

Dts.TaskResult = (int)ScriptResults.Success;
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 the location you specified.

Loading

Sifiso

October 13, 2022
Sifiso is Data Architect and Technical Lead at SELECT SIFISO – a technology consulting firm focusing on cloud migrations, data ingestion, DevOps, reporting and analytics. Sifiso has over 15 years of across private and public business sectors, helping businesses implement Microsoft, AWS and open-source technology solutions. He is the member of the Johannesburg SQL User Group and also hold a Master’s Degree in MCom IT Management from the University of Johannesburg.

Meet Our Experts

We are proud to have a team of experts who are passionate about delivering the best possible solutions to you. Our team members are highly skilled and experienced in a diverse range of IT technologies, and are committed to staying up-to-date with the latest industry trends and best practices to deliver you the best results.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join Our Newsletter

Subscribe to get our latest and best thinking on the most definitive workforce topics affecting HR leaders and organizations today.