Programmatically Insert into SQL Server Table from an Excel File Using SSIS Script Tasks

By | January 30, 2012

Abstract

This article explains how to Programmatically create an SQL Server Integration Services (SSIS) package with a 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 System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

After declarations, create an instance of application, workbook, workshet and range:

public void Main()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;

string INS_str =””;

string str;
double str2;
string str3;
int rCnt = 0;
int cCnt = 0;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(“C:\\your_excel_file.xls”, 0, true, 5, “”, “”, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, “\t”, false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;

Establish a connection to your SQL Server instance:

SqlConnection connection = new SqlConnection(string.Format(“Data Source={0};Initial Catalog={1};Integrated Security=SSPI;”, “your_sql_server”, “your_sql_table”));
SqlCommand command;
connection.Open();

for (rCnt = 2; rCnt {
for (cCnt = 1; cCnt 1)
{
if (str.Contains(“‘”))
{
INS_str = INS_str + “‘” + str.Replace(“‘”, “””) + “‘,”;
}
else
{
INS_str = INS_str + “‘” + str + “‘,”;
}
}
else
{
if (str.Contains(“‘”))
{
INS_str = “‘” + str.Replace(“‘”, “””) + “‘,”;
}
else
{
INS_str = “‘” + str + “‘,”;
}
}
}

Exception handling:

catch (Exception exc)
{
if (exc.Message.Contains(“”))
{
str2 = Convert.ToDouble((range.Cells[rCnt, cCnt] as Excel.Range).Value2);
str3 = Convert.ToString(str2);
}
}

}
}

xlWorkBook.Close(true, null, null);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show(“Unable to release the Object ” + ex.ToString());
}
finally
{
GC.Collect();
}
}

We then save the package into a file system.

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

Conclusion

It’s that simple!

You can now execute your script task and the data will be inserted into your destination table.

2 thoughts on “Programmatically Insert into SQL Server Table from an Excel File Using SSIS Script Tasks

  1. Dnyaneshwar

    Hi Sifiso,

    I am new to C#. I used above mentioned code in script task. However I am getting syntax errors in for loop. I am not sure where am I doing wrong. Could you please elaborate what exactly is happening in this for block? Thanks.

    Reply

Leave a Reply