Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying using to a SqlDataReader

Tags:

vb.net

using

I just want to verify something. I believe it is likely that if I apply the using command to a SqlDataReader, that it will both close the data reader and dispose of it. For example:

Using sdr As SqlDataReader = cm.ExecuteReader()
   Dim someInt As Integer = sdr.GetInt32(0)
   'other details and actions
End Using

Will that close the sdr SqlDataReader after it exits the Using code block. (I believe it will, but just want to verify.)

like image 256
Rod Avatar asked Dec 23 '09 15:12

Rod


People also ask

What is the use of SqlDataReader?

The SqlDataReader is used to read a row of record at a time which is got using SqlCommand. It is read only, which means we can only read the record; it can not be edited. And also it is forward only, which means you can not go back to a previous row (record).

Do I need to close SqlDataReader?

You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.

What is difference between SqlDataReader and SqlDataAdapter?

A SqlDataAdapter is typically used to fill a DataSet or DataTable and so you will have access to the data after your connection has been closed (disconnected access). The SqlDataReader is a fast forward-only and connected cursor which tends to be generally quicker than filling a DataSet/DataTable.

What does SqlDataReader return?

As explained earlier, the SqlDataReader returns data via a sequential stream. To read this data, you must pull data from a table row-by-row Once a row has been read, the previous row is no longer available.


2 Answers

Yes. Using calls IDisposable.Dispose, and the MSDN page on SqlDataReader.Dispose says:

SqlDataReader.Dispose Method

Releases the resources used by the DbDataReader and calls Close.

like image 169
Heinzi Avatar answered Sep 19 '22 12:09

Heinzi


Yes, the reader will be closed when it is disposed. From the SqlDataReader.Dispose documentation:

Releases the resources used by the DbDataReader and calls Close.

like image 32
Jeff Sternal Avatar answered Sep 22 '22 12:09

Jeff Sternal