Is the SqlClient.SqlDataReader a .NET managed object or not? Why do we have to call the Close() method explicitly close an open connection? Shouldn\'t running out of scope f
@Lieutenant Frost
As a rule in our shop, we explicitly wrap all database calls in a Try...Finally block, with the finally section catching and closing the data connections. It's worth the tiny bit of effort to save yourself a major troubleshooting headache.
I have a similar rule, but I require that objects implementing IDisposable use the 'using' block.
using (SqlConnection conn = new SqlConnection(conStr))
{
using (SqlCommand command = new SqlCommand())
{
// ETC
}
}
The using block calls Dispose immediately when leaving the scope, even with an exception.
Also take into consideration what happens when an exception gets thrown - you never know if the connection will be closed if you suddenly are forced out of the executing code.
As a rule in our shop, we explicitly wrap all database calls in a Try...Finally block, with the finally section catching and closing the data connections. It's worth the tiny bit of effort to save yourself a major troubleshooting headache.