Differences between DataReader
Model and DataSet Model
Data in ADO.NET is disconnected for all practical purposes. Data access can be
broken down into two methods, or models.The DataSet model involves reading
the data into a local cache, interacting with it, and discarding, or synchronizing,
the data back to the source.The DataReader model does not allow for updating
data or reusing it.With a DataReader, data is read once and discarded when the
next row is read.
When you populate a DataSet from the database, a connection is opened, the
data is selected and returned into a DataTable, and then the connection is closed.
The data is present in the DataTable, and an application is free to interact with itread.
When you populate a DataSet from the database, a connection is opened, the
data is selected and returned into a DataTable, and then the connection is closed.
The data is present in the DataTable, and an application is free to interact with it
in any manner, however, the database is free to do whatever it needs to do.
Resources are not being held on the database server while the application is
being used.
When a DataReader is used for data access, a connection is opened, and the
data is navigated using the Read method. It is not possible to “go back” and read
data that has previously been read, or rather it is not possible to scroll backward
in the data. Because a DataReader is forward-only and read-only, it is useful only
for retrieving the data and is very efficient.You need to realize that during the
scrolling process, resources are being held up on the server.This means that if an
application allows a user to manually navigate in a forward-only manner, the
database is serving the request and waiting.This may result in a resource problem
at the database. It is best to use the DataReader when fast access to the data is
needed, and the entire resultset is being consumed in a relatively short period of
time.This, of course, depends on several variables, such as number of users,
amount of data, hardware availability, and so on.
In both instances, the data is retrieved; however, with the DataSet it is persisted
in a DataTable. As stated earlier, a DataReader is used to populate a
DataTable, so in this regard if a developer needs to access the data once in a forward-
only mode, the DataReader provides a faster mechanism. On the other
hand, if this data is somewhat expensive to create, and it will be used repeatedly,
using a DataSet makes more sense.These are the types of decisions that you will
need to make during the course of designing the application.
The two models are similar in that they both provide data, but that is where
the similarities end.The DataReader provides a stream of data, whereas the
DataSet provides a rich object model with many methods and properties to
interact with the data in any scrolling direction an application would need.