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.)
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).
You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With