SqlConnection cnn = new SqlConnection();
cnn.Open();
ds = new
System.Data.DataSet();
da.Fill(ds);
cnn.Close();This does not ensure that cnn will get closed if da.Fill(ds) fails.
Good:
SqlConnection cnn = new SqlConnection();
try
{
cnn.Open();
ds = new System.Data.DataSet();
da.Fill(ds);
}
catch
{
throw new Exception("Failed");
}
finally
{
if (cnn != null)
cnn.Close();
}
Better:
using (SqlConnection cnn = new SqlConnection())
{
cnn.Open();
ds = new System.Data.DataSet();
da.Fill(ds);
}
No comments:
Post a Comment