Import Text and CSV Files into SQL Server Database with SSIS Script Task

By:   |   Comments (12)   |   Related: More > Integration Services Development


Problem

There are several techniques available to import and export data for SQL Server. In addition, there are free SQL tools and utilities available that might be helpful for specific use cases. In SQL Server Integration Services (SSIS), we can use a Flat File Source to load Text/CSV files. Despite this, I would like to demonstrate how to import Text/CSV files using the Script Task.

Solution

Why use a Script Task in SSIS to import text data?

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.

How to configure a Script Task in SSIS

In order to configure the SQL Server Integration Services package for a Script Task, we need to understand the following:

  • Specify the custom script to meet business needs.
  • Choose the method in the VSTA project which is Integration Services runtime calls.
  • Specify the language for script.
  • Specify readonly/writeonly variables for use in the script.

Here are the steps.

Step 1: Sample SQL Server Database Script

The following script creates a database SSIS_FileData and sample table Customer_Data.

Use Master 
GO
 
Create Database SSIS_FileData 
GO 

USE SSIS_FileData 
GO 

CREATE TABLE Customer_Data 
( 
  Name Nvarchar(400), 
  city nvarchar(200), 
  Address nvarchar(1000) 
) 
GO 

Step 2: Prepare Source files for reading

I have two types of files, CSV and text as shown below.

Source files

These files contain the below data.

Source files format

"NG_Customer" is CSV file which contains comma delimited data and "SG_Customer" is a text file which contains tab delimited data. Currently, in this folder I have only two files, but it is possible to increase the number of files in the folder.

Step 3: Configure Project

Open Visual Studio and execute these steps:

In the menus select, File > New > Project.

I created a new SSIS project and named it "Importing_Data_In_SQL_From_TextFile_Using_ScriptTask".

Creating a project

After creating a project, in the Solution Explorer there is a default package.dtsx. You can right click on package.dtsx and rename as I did and as shown below.

Changing to package name

Step 4: Configure ADO.NET Connection to Import Text/CSV Data into Table

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.

initiated globally ADO.NET Connection

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.

Assign Connection Property

In the Connection Managers tab, I renamed the new connection to DBConnection.

Assign name to ADO.NET Connection

Step 5: Configure SSIS Variable

Create Variable: FilePath

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.

Taking a local variable Filepath to getting a files from directory

Create Variable: TableName

I am going to add a variable TableName and configure it as shown below.

Taking a local variable TableName for dumping the data.

Step 6: Configure Script task in SSIS

Drag a Script Task from the SSIS Toolbox to the Control Flow.

Taking a Script task

Double click on the Script Task and the Script Task Editor will open. Set the ReadOnlyVariables to FilePath, TableName as shown below.

Assigned readonlyvariables to script task

Now I am going to click on Edit Script and enter the code.

Display visual studio script editor to write 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.
Added Namespaces

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.

Adding exception Handling 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;
   }
}

Variable Initiated in a Script Task

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.

string FilePath = Dts.Variables["User::Filepath"].Value.ToString(); 
string TableName = Dts.Variables["User::TableName"].Value.ToString(); 

I created an array for the multiple source files.

string [] fileEntries = Directory.GetFiles(FilePath, "*"); 

To read the file by line, I used local variable Line.

string Line = string.Empty;

To prepare the query for importing the data to the destination table I used a local variable query.

string query = string.Empty; 

Configure a SQL Connection in Script Task

I used the following code to use the "DBConnection" for the connection that we setup in step 4.

SqlConnection conn = new SqlConnection(); 
conn = (SqlConnection)(Dts.Connections["DBConnection"].AcquireConnection (Dts.Transaction) as SqlConnection); 

Process to Loop Through Files and Load to Table

Below are the steps to loop through the files and then load each file.

Foreach iteration to reading the files and code is prepared to dumping files data into SQL Server.

Execute SSIS Package

My package is ready for execution. Now I am going to execute it.

Package Execution

The package executed successfully and here is the data in destination table "Customer_Data" which imported data from both files (Text and CSV) "NG_Customer", "SG_Customer".

After reading the files, getting the data from SQL Server Table.

Conclusion

  • To check the code that is generated I used a MessageBox in the Script Task which is commented out. Note, you can’t use a MessageBox in a production environment because the package waits while the MessageBox is open.
  • I used a global SQL Connection in the Script Task, but you can also create your own SQL Connection in the Script Task if needed.
  • I hardcoded the database name in the Script Task, but this can also be configured to pull from a variable.
Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Bhavesh Patel Bhavesh Patel is a SQL Server database professional with 10+ years of experience.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Tuesday, February 27, 2024 - 3:25:23 AM - Saleem Back To Top (92014)
Thank you for sharing this article.
Can you give scripts to read correct format of "lastlogontimestamp" from Active Directory and insert into SQL DB?

Friday, June 23, 2023 - 4:38:52 PM - sonam Back To Top (91336)
how do we handle if data has comma and it is srounded by double quotes? how do we consider that and load the data?

Friday, June 10, 2022 - 12:44:23 PM - Aldo Támara Back To Top (90154)
Thank you for sharing this article, its good.

Monday, April 4, 2022 - 9:21:56 AM - Srikanth Back To Top (89964)
Can anyone explain how to format the query with 20 fields,i cant able to understand how to sent those many fields to the query. If anyone had a solution please let me know

Friday, November 19, 2021 - 7:09:19 AM - Krunal Shashikant Kathwate Back To Top (89468)
Is there anyway in which we can get the same results but with files having a bit of different columns? Please assist. Will be grateful.

Monday, July 12, 2021 - 9:20:12 AM - aravind Back To Top (88992)
Hi Team,

ScriptResults does not exist in the current context, how to fix this??

Tuesday, March 30, 2021 - 8:35:05 AM - Slao Back To Top (88472)
Hi, how to avoid importing duplicated data from the same files when uploading netx times?

Monday, March 8, 2021 - 10:36:50 AM - Nic Abbott Back To Top (88357)
Hi Bhavesh

I was trying to do exactly what you have described here with no previous knowledge of SSIS. I tried using Access but quickly hit teh 2gb limit. I also haven't written in C# before, so that you for the clear description. Your line by line expanation of a complex situation has helped me greatly. I had fixed width text files to read (actualy 281 of them) so I have to do my own parsing of the lines with a few extra variables and some simple substring statements, but otherwise it is working. It's going to be a few hours to read all the files into SQL Server, but thank you for this tutorial.

Cheers
Nic

Monday, September 7, 2020 - 10:54:53 AM - Vinod Back To Top (86429)
Good Place of BI Knowledge. If we are searching for an issue if this link found we will feel we got the Answer... :)

Friday, February 14, 2020 - 10:11:21 AM - venkata Back To Top (84485)

Suppose if file having 5 feilds like (A,B,C,D,E) but target table having only 3 columns so we need to load A,B,D FEILDS FROM file only.


Saturday, September 21, 2019 - 8:53:31 AM - Bhavesh Patel Back To Top (82533)

Hi,

I have a fixed file format which is mentioned and there is not any case to generate an empty file in my environment.

However, if you need to such checking then refer to this link.

https://docs.microsoft.com/en-us/sql/integration-services/extending-packages-scripting-task-examples/detecting-an-empty-flat-file-with-the-script-task?view=sql-server-2017


Monday, September 16, 2019 - 5:30:40 AM - Davison Gaitán Escobar Back To Top (82465)

Hello, how do you handle text qualifier issue and empty files with script task?















get free sql tips
agree to terms