Closing of data reader..............

  • Thread starter Thread starter Lowry Smith
  • Start date Start date
L

Lowry Smith

MyReader.ExecuteReader(CommandBehavior.CloseConnection)



I am using the above statement for the reader object. Closing of connection
object is taken care in the above statement. Do I have to close the 'Reader'
exclusively?

..Net 1.1.

Thank you,

Smith
 
Lowry Smith said:
MyReader.ExecuteReader(CommandBehavior.CloseConnection)
I am using the above statement for the reader object. Closing of connection
object is taken care in the above statement.

Do I have to close the 'Reader' exclusively?

Yes, if the SqlDataReader is created with CommandBehavior set to CloseConnection, closing the SqlDataReader closes the connection automatically.
http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.CarlProthman.NET
 
Carl Prothman said:
Note there is one exception to this rule when using
CommandBehavior.CloseConnection. When you bind the SqlDataReader to an
ASP.NET control (e.g. DataGrid), the connection is closed after the
ExecuteReader method has returned all of the records.

Isn't that always the case? I mean you can close a SqlDataReader only when
it has fetched all records. If you close it before, it will still fetch all
records and then close.
 
I think I was not clear about my question.
The question is, whether I have to write the below statement or not?
MyReader.Close()
Since I am using 'CommandBehavior.CloseConnection', once the data are
fetched completely, the connection is closed. And alll the associated
resources are released (in this case Data Reader)??.
Now, Is it necessary to call MyReader.Close?
Thank you for your answers,
Smith
 
Yes, why not?
CloseConnection means to close the connection when the reader is closed -
and unless you dispose or/and close datareader it isn't closed at that time.
 
Back
Top