cmd.connection.dispose() will dispose of the connection. you still need to dispose of the command. use the using keyword to dispose of objects. this is a shorthand for try/finally blocks. example.
this
Code:
using (IDbConnection cnn = new SqlConnection())
{
IDbCommand cmd = new SqlCommand("select * from foo", cnn);
cmd.ExecuteReader();
}
equals this
Code:
IDbConnection cnn = new SqlConnection()
IDbCommand cmd = new SqlCommand("select * from foo", cnn);
try
{
cmd.ExecuteReader();
}
finally
{
cmd.Dispose();
cnn.Dispose();
}
any objects declared within the using scope will be deallocated upon exiting the block.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.