1.

How Do You Ensure That The Database Connections Are Always Closed?

Answer»

To ensure that the database CONNECTIONS are ALWAYS closed, open the connection inside of a using block, as SHOWN in the following code fragment. Doing so ENSURES that the connection is automatically closed when the code exits the block.

using (SqlConnection ConnectionObject = NEW SqlConnection())
{
ConnectionObject.Open();
//The database connection will be closed when the control exits the using code block
}

To ensure that the database connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.

using (SqlConnection ConnectionObject = new SqlConnection())
{
ConnectionObject.Open();
//The database connection will be closed when the control exits the using code block
}



Discussion

No Comment Found