21

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.'"

2 Answers 2

29

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);
Sign up to request clarification or add additional context in comments.

9 Comments

If I wanted to get a value (in my case I'm trying to get a DateTime) out of the sqlComm, would I just use something like: DbDataReader reader = sqlComm.ExecuteReader() - and then cycle through the whatever comes out?
By changing the database connection from OLEDB to ADO.Net, the code listed above worked for me.
@Tequila. You would have to use Oledb objects to connect using an Oledb connection eg..System.Data.OleDbOleDbConnection and OleDbCommand...
@Diego - You deserve a gold medal for this post. This saved me tons of time. Btw, where can I get information about these kind of things ? Was this thing mentioned in some MS documentation ?
Note this will not work with OLEDB, per the Microsoft Docs, and all of this is documented here: learn.microsoft.com/en-us/sql/integration-services/…
|
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.