Using using to dispose of nested objects
Posted
by TooFat
on Stack Overflow
See other posts from Stack Overflow
or by TooFat
Published on 2010-04-20T18:32:51Z
Indexed on
2010/04/20
18:43 UTC
Read the original article
Hit count: 244
c#
|idisposable
If I have code with nested objects like this do I need to use the nested using statements to make sure that both the SQLCommand and the SQLConnection objects are disposed of properly like shown below or am I ok if the code that instantiates the SQLCommand is within the outer using statement.
using (SqlConnection conn = new SqlConnection(sqlConnString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdTextHere;
conn.Open();
cmd.Connection = conn;
rowsAffected = cmd.ExecuteNonQuery();
}
}
© Stack Overflow or respective owner