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

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
 
C

Carl Prothman

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
 
M

Miha Markic [MVP C#]

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.
 
L

Lowry Smith

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
 
M

Miha Markic [MVP C#]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top