The two objects are to be used in contexts fundamentally different.
A DataReader
instance returned by the ExecuteReader
doesn't return anything until you loop over it using the Read()
method. It is a connected object that has a pointer to a current record on the backend database. You read the content of the record using the various GetXXXXX
methods provided by the reader or simply using the indexer. When you have done with the current record you orderly pass to the following one using the Read() method. No way to go back or jump to record N + 100.
A DataSet
instead is a disconnected object. It uses internally a DataReader
to fill its local memory buffer with all the records returned by the command text query. It is handy if you need to work randomly on the data returned or show them on video or print them. But of course, waiting to have millions of records returned by the internal reader could be time consuming and the consuming of the local memory probably will kill your process before the end.
So, which is the best? Neither, if you have millions of records in your table, you need to put in place an appropriate WHERE
condition to reduce the amount of records returned. Said that, it depends on what you need to do with the returned data. To display them on a grid probably you could use a DataSet. Instead a DataReader is better if you need to execute operations on the records one by one.