简体   繁体   中英

Error when Utilizing SSIS ADO.NET Connection from within C# Script Task

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;
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM