Professional Documents
Culture Documents
A Script Task is one of the best components which is scalable to use in SSIS. Also, you can add new functionality using C# or VB.
There are many use cases to meet business needs using Script Tasks, such as:
• Using the Script Task, we can use variables; for example, we can pass variable values between SSIS packages
• We can easily get information from Active Directory using a Script Task.
• We can read/write files like text, Excel, etc.
• When working with images, the Script Task can be helpful.
• Using the Script Task, we can import/export data from the database engine to the file system and vice versa.
• We can perform tasks using a Script Task like logging, raising errors, warnings or informational messages.
• We can also send HTML messages using a Script Task.
Before using a Script Task, we need to know how to run a script and the computer must have Microsoft Visual Studio Tools for
Applications (VSTA) installed. I only installed SSDT (SQL Server Data Tools) which installs a shell of Visual Studio which can execute a
Script Task.
The following script creates a database SSIS_FileData and sample table Customer_Data.
Use Master
GO
USE SSIS_FileData
GO
In order to import data into the table, I need a SQL Connection in the package. There are two ways to create a connection manager in
SSIS: Package Level and Project Level.
With the Package Level option, we need to create a connection manager in every package within a project and when a connection
manager is created at the Project Level then the connection is available for all packages in the project.
I prefer to use Project Level connections. Right click on Connection Managersand click on New Connection Manager and a list of
connection manager types will pop up.
I choose ADO.NET (ActiveX Data Objects) and clicked the Add button. The following screen, Configure ADO.NET Connection
Manager, opens where you can configure the connection.
In the Connection Managers tab, I renamed the new connection to DBConnection.
Step 5: Configure SSIS Variable
In order to get the files from the directory, I needed the source folder path. For the purpose of setting a dynamic path, I am going to use
a variable to do this. Right click in the Control Flow and select the Variables tab. Now click on Add Variable and assign the Name, Data
Type and Value respectively.
Create Variable: TableName
Drag a Script Task from the SSIS Toolbox to the Control Flow.
Double click on the Script Task and the Script Task Editor will open. Set the ReadOnlyVariables to FilePath, TableName as shown
below.
Now I am going to click on Edit Script and enter the code.
First, I am going to add two needed Namespaces in Namespaces area.
• For the purpose of getting the files from directory, I use namespace System.IO.
• For the purpose of importing the data into SQL Server, I use namespace System.Data.sqlclient.
I put a Try-Catch block for exception handling in the Script Task. In addition, I used an error raising event FireError in the catch block.
Now I am going to write the code as shown below and I will explain the code as well.
public void Main()
{
try
{
Int32 ctr = 0;
string FilePath = Dts.Variables["User::Filepath"].Value.ToString();
string TableName = Dts.Variables["User::TableName"].Value.ToString();
string[] fileEntries = Directory.GetFiles(FilePath, "*");
string Line = string.Empty;
string query = string.Empty;
SqlConnection conn = new SqlConnection();
conn = (SqlConnection)(Dts.Connections["DBConnection"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Read data from table or view to data table
foreach (string fileName in fileEntries)
{
System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName);
ctr = 0;
while ((Line = SourceFile.ReadLine()) != null)
{
if (ctr != 0)
{
Line = Line.Trim();
query = "Insert into SSIS_FileData.dbo." + TableName + " values('" + Line.Replace(",", "','").Replace(" ", "','") + "')";
SqlCommand SQLCommand = new SqlCommand(query, conn);
SQLCommand.ExecuteNonQuery();
////MessageBox.Show(query);
}
ctr++;
}
}
Dts.TaskResult = (int)ScriptResults.Success;
conn.Close();
}
catch (Exception ex)
{
Dts.Events.FireError(0, "Exception from Script Task", ex.Message + "\r" + ex.StackTrace, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
To ignore the file header when reading the input files, I am using variable ctr.
Int32 ctr = 0;
For setting the input variables values, I used the local variables for the source directory and destination table.
To prepare the query for importing the data to the destination table I used a local variable query.
Below are the steps to loop through the files and then load each file.