I have the code below to query records from a stored procedure but am concerned I may not be disposing what I need to or am disposing when the object would be cleared by the
You should dispose the data reader, and the command. No need to separately close the connection if you dispose the command. You should ideally do both using a using
block:
using (SqlCommand cmd = new...)
{
// do stuff
using (SqlDataReader dr = cmd.ExecuteReader())
{
// do stuff
}
}
If you need exception handling do that separately either inside or around the using blocks - no need for the finally for the Dispose
calls though with using
.