Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SQL database inside Script Task in SSIS

Inside of a Script Task in SSIS, I need to make a call to an SQL database. I have a connection string that was created when I added the database to the data sources folder, however now I'm not sure how to reference it inside the C# code. I know how to do this in the code behind of an ASP website, however it seems that SSIS should have a more direct method.

EDIT

This line of code actually winds up throwing an exception:

sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);

It reads: "Unable to cast COM object of type 'System._ComObject' to class type 'System.Data.SqlClient.SqlConection.'"

like image 590
NealR Avatar asked Aug 08 '12 15:08

NealR


People also ask

How does SSIS connect to SQL database?

In SQL Server Data Tools (SSDT), open the Integration Services project. In Solution Explorer, right-click Connection Managers, and click New Connection Manager. In the Add SSIS Connection Manager dialog box, select the type of connection manager, and then click Add.

What can be used in SSIS script task?

Two programming languages are supported in the SSIS Script task: Visual Basic . NET and C#.


2 Answers

you cant use the configurations from a connection manager from inside a script task like: conectionManager1.exceuteSQLStatment(...)

once you are "inside" the script task you need to access the CM like a variable:

ConnectionManager cm;
System.Data.SqlClient.SqlConnection sqlConn;
System.Data.SqlClient.SqlCommand sqlComm;

cm = Dts.Connections["conectionManager1"];

sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
sqlComm = new System.Data.SqlClient.SqlCommand("your SQL Command", sqlConn);
sqlComm.ExecuteNonQuery();

cm.ReleaseConnection(sqlConn);
like image 95
Diego Avatar answered Oct 18 '22 19:10

Diego


You must check the privider of the connection that your are trying to instantiate.

You can't cast a OleDb.OleDbConnection connection as a SQLClient.SQLConnection, the parameters are different.

like image 1
Hugo Vares Avatar answered Oct 18 '22 19:10

Hugo Vares