Can I place the using() statement only on the SqlConnection? Will it also free the child SqlCommand and SqlDataReader?
Can I do this:
using(SqlConnection connection = new SqlConnection("connection string"))
{
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection);
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
//do something
}
}
}
Or does it need to be:
using(SqlConnection connection = new SqlConnection("connection string"))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
//do something
}
}
}
}
}