DbCommand and using statement

  • Thread starter Thread starter offwhite
  • Start date Start date
O

offwhite

I would like some feedback on the following code snippet. It is using
the Data Access Application Block where db is a Database object. What
I want to know is how I should be using the "using" statement to scope
the use of the DbCommand. This technique should allow me to avoid
disposing the variables in the finally block after the try...catch
blocks.

Is this how you would do it? Are there any gotchas here?

public DataSet GetData()
{
DataSet ds = new DataSet();

try
{
using (DbCommand dbCmd =
db.GetStoredProcCommand("sp_GetData"))
{
ds = db.ExecuteDataSet(dbCmd);
}
}
catch (Exception ex)
{
// handle the exception
}

return ds;
}

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Looks fine to me.
Perhaps you could move try block inside using block (depends on what you
want to catch).
 
Back
Top