Possible Duplicate:
in a “using” block is a SqlConnection closed on return or exception?
Would this using close this _connection?
using(SqlConnection _connection = Class1.GetSqlConnection())
{ //code inside the connection
}
//connection should be closed/ended?
I’m just wondering because GetSqlConnection() is a static function of Class1 and the whole connection might not be closed because it is calling outside class’ static function instead of straight?
using(SqlConnection _connection = new SqlConnection(_connectionString)
{ //code inside the connection
}
Yes it does.
Here is an alternative way to implement the
GetSqlFunctionmethod so that you have a more explicit behaviour:Then you call it like so:
You could then extend this method to use an existing connection or create a new one depending on the semantics you prefer.