Possible Duplicate:
Will a using block close a database connection?
Is db.Close()
unnecessary in the following?
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close();
}
Is there any need to close a DbConnection if a using clause is used?
No, there is no need to close a DbConnection if a using clause is used?
and
Yes it is unnecessary in here because when scope of using
ends, connection will dispose meaning closing and releasing all memory.
Since DBConnection
implements IDisposable
interface, close function is there in the Dispose
method of DBConnection
.
But if some lines are after close line then it is useful
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close(); //Useless
}
But here it is useful
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
db.Close(); //Useful
// Some more code
}
In that case you can do
using (DbConnection db = GetDbConnection())
{
// do data-access stuff
// ...
}
// Some more code which was previously inside using section.