0

I am using IDataReader to get the data from db using stored procedure. ie something like this

using (IDataReader Reader = SqlHelper.ExecuteReader(ConnectionString, "StoredProc1", sqlParam))
{
     while (Reader.Read())
     {
     }
}

In that case is it required to close the reader manually? my doubt is since we are using the Using directive, after execution whether it will close the reader automatically?.

Thanks,

Mahesh

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
Mahesh KP
  • 6,248
  • 13
  • 50
  • 71

1 Answers1

2

Since you're using using, and the returned reader implements IDisposable, Dispose() will be called automatically. Assuming Dispose() is correctly implemented (and it is), it will do whatever is necessary to make sure the object can be safely disposed of. If that requires calling Close() it will call Close; Or an internal equivalent we don't need to know about.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
  • So it will close() and Dispose() reader automatically. ie so we didn't have to bother about it. am i right? – Mahesh KP Apr 18 '11 at 11:34
  • @mahesh, it will call `Dispose()`, it will not call `Close()`. But Dispose will do *everything* that's needed to properly release all resources used, so one way or another it will do what `Close()` does. I don't want to say `Dispose()` is going to call `Close()` (those there's a good chance it actually does) because that's an implementation detail. – Cosmin Prund Apr 18 '11 at 13:06