Sunday 29 March 2009

Using keyword

When using anything that implements IDisposable (SqlDateReader, SqlConnection etc) you can use the using keyword to ensure that the object is disposed, e.g.

using (SqlDataReader reader = Dal.ExecuteReader(GetConnection(), "SelectProductAll"))
{
dsScrum.ProductTask.Clear();
if (reader.HasRows)
dsScrum.Product.Load(reader);
}



Is the same as:

SqlDataReader reader = Dal.ExecuteReader(GetConnection(),"SelectProductAll");
try
{
dsScrum.ProductTask.Clear();
if (reader.HasRows)
dsScrum.Product.Load(reader);
}
finally
{
if (reader != null)
reader.Dispose();
}

No comments:

Free Hit Counter