I use the using statement for SqlConnection
. It's is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.
However, I realized that object created in using cannot be redefined. I cannot do like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
I was wondering if I can replace using, and do something like this:
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
The SqlConnection
will not be accesible after last }
brace. Will be the Dispose() called immediatly when object goes out of scope?