1

I'm getting the error, "The connection was not closed. The connection's current state is open." when executing the conn.Open(); command in the block below in a C# script task in my SSIS package. In Googling, I've seen other people saying that this may result from a try / catch not leading to the conn.Close();, but shouldn't "using" dispose the connection for me when it's finished?

bool fileRecordExists;
using (SqlConnection conn = (SqlConnection)Dts.Connections["connectionName"].AcquireConnection(Dts.Transaction))
{
    SqlCommand sqlCmd = new SqlCommand(queryString, conn);
    conn.Open();
    fileRecordExists = (int)sqlCmd.ExecuteScalar() > 0 ? true : false;
}

2 Answers 2

2

SSIS opens the connection for you. What I usually do, right or wrong, is create a new connection using the connection string which can be acquired very similar to how you are getting the active connection.

I have also, on occasion, used the AcquireConnection method but do not open or close the connection in my scripts.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that got me past the first error. But the password for the account we're using is not stored in the connection string - so authentication is failing when trying to open the connection. I'm gonna try acquiring the active connection.
1

This will help. The Connection Object is a part of ADO.NET Data Provider and it is a unique session with the Data Source. The Connection Object is Handling the part of physical communication between the C# application(task) and the Data Source.

The Connection Object connect to the specified Data Source and open a connection between the C# application and the Data Source, depends on the parameter specified in the Connection String. When the connection is established, SQL Commands will execute with the help of the Connection Object and retrieve or manipulate data in the Data Source.

Once the Database activity is over, Connection should be closed and release the Data Source resources.

2 Comments

Thanks for the information! Concerning your last line, do I need to manually release / close the connection? My code is currently as simple as.: string queryString = "SELECT COUNT(*) FROM table WHERE Filename = '" + filenametoprocess + "'"; SqlConnection conn = (SqlConnection)Dts.Connections["SSISDataConnection"].AcquireConnection(null); SqlCommand sqlCmd = new SqlCommand(queryString, conn); bool fileRecordExists = (int)sqlCmd.ExecuteScalar() > 0 ? true : false; Should I add conn.Close()? Do I need to release anything?
Yes.. That is best practice for any db connection. Closing connection will free up the resource, Every DB has max number of connection open. msdn.microsoft.com/en-us/library/ms187030.aspx

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.