Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some of the advantage/disadvantages of using SQLDataReader?

Tags:

.net-2.0

SqlDataReader is a faster way to process the stored procedure. What are some of the advantage/disadvantages of using SQLDataReader?

like image 516
jbcedge Avatar asked Oct 10 '08 18:10

jbcedge


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

Why would a developer choose SqlDataAdapter over a SqlDataReader?

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.

Which is faster SqlDataAdapter and SqlDataReader?

DataAdapter gets all the rows of the executed SQL statement at once and populates into DataSet or DataTable in memory and hence DataAdapter is bit slower compared to DataReader.

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.


1 Answers

I assume you mean "instead of loading the results into a DataTable"?

Advantages: you're in control of how the data is loaded. You can ask for specific data types, and you don't end up loading the whole set of data into memory all at the same time unless you want to. Basically, if you want the data but don't need a data table (e.g. you're going to populate your own kind of collection) you don't get the overhead of the intermediate step.

Disadvantages: you're in control of how the data is loaded, which means it's easier to make a mistake and there's more work to do.

What's your use case here? Do you have a good reason to believe that the overhead of using a normal (or strongly typed) data table is significantly hurting performance? I'd only use SqlDataReader directly if I had a good reason to do so.

like image 150
Jon Skeet Avatar answered Oct 20 '22 09:10

Jon Skeet