Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DataReader works?

I was thinking that the SQLDataReader should not work if there is no connection to the SQLServer.

I experimented this scenario. I execute the ExecuteReader then stop the SQLServer Service and tried to iterate through the DataReader. What I expected was an exception, but it gave the results one after the other. Ideally the DataReader should read one row at a time from the stream that gets connected to the DB server and which should throw an exception if we disconnect the DB server?

I don't know, What is it that I am missing here.

like image 440
Nirajan Singh Avatar asked Sep 05 '09 18:09

Nirajan Singh


People also ask

How does SQL DataReader work?

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.

Which is DataReader method?

A DataReader parses a Tabular Data Stream from Microsoft SQL Server, and other methods of retrieving data from other sources. A DataReader is usually accompanied by a Command object that contains the query, optionally any parameters, and the connection object to run the query on.

Is DataReader faster than DataSet?

The DataSet represents a complete set of data among the tables that include related tables, constraints, and relationships. However, this greater functionality comes with performance overhead; therefore, DataSet is slower than DataReader.


1 Answers

I strongly suspect that the reader reads a batch of results at a time. That's a lot more efficient than one row at a time (think about the situation where a single row is only a few bytes... you don't want a network packet per row when it could have retrieved lots of rows in a single packet). It also potentially allows the database to release its internal resources early - if the data reader has read all the results (when there are only a few) it can effectively forget about the query.

I suspect if you try the same type of thing with a query returning lots of results, you'd get the expected exception.

like image 106
Jon Skeet Avatar answered Oct 03 '22 21:10

Jon Skeet