Mahesh KP
Mahesh KP

Reputation: 6446

IDataReader to execute stored procedures

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

Upvotes: 0

Views: 313

Answers (1)

Cosmin Prund
Cosmin Prund

Reputation: 25678

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.

Upvotes: 2

Related Questions