Monday, October 8, 2007

DataSet Vs SqlDataReader

DataSet
DataSet object will be populated by a SqlDataAdapter object.Below are the requirements where DataSet is more appropriate.
1.When you require a disconnected in memory of data.
where you can pass it to another tier/layer of your application.
2.Where we require an in-memory relational view of the data.(when you retrieve data from multiple tables/sources, you can have relations between DataBase objects in DataSet.)
3.SqlDataAdapter provides batch update mechanism.
4.When you want to perform data binding with a control that supports a data source that implements IList.
Notes:
1.SqlDataAdapter automatically handles the connection open and close.
2.If you open the connection, SqlDataAdapter won't close it for you. it is developer responsibility to close the connection.

I guess SqlDataAdapter won't bother about the connection that you opened. it just picks another connection from the pool and uses it and closes once its done. The connection that you opened explicitly is your baby so you need to take care about it.
SqlDataReader
SqlDataReader object will be created by calling the ExecuteReader method of the
SqlCommand object.
Below are the scenarios where DataReader is appropriate
1.When you are dealing with large volumes of data, Where its tough to maintain in a single cache.
2.When you want to avoid the object creation(DataSet, DataTable, Rowcolection etc) overhead associated with the DataSet.
3.When you want to perform data binding with a control that supports a data source that implements IEnumerable.

Note: The SqlDataAdapter internally uses SqlDataReader to populate data into DataSe/DataTable.

Here are some difference in table format.


















































DataReader DataSet
A DataReader is specific to a data provider (for
example, SqlDataReader, OdbcDataReader, and OleDbDataReader).
The DataSet class isn’t a part of any data provider.
It’s specific to .NET only. However, the DataAdapter used to fill the DataSet
with Fill() is specific to a data provider.
The data retrieved through a DataReader is read-only.
The data retrieved through a DataSet is read-write.
The data retrieved through a DataReader is
forward-only. Once the data has been cycled through, the DataReader must be
closed and re-created in order to reaccess the data.
You can work with data in a DataSet in any order you
choose as many times as you like.
A DataReader presents data through a direct
connection to the data source. Only one row of data is stored in Internet
Information Services (IIS) memory at any one time.
A DataSet stores all the data from the data source in
IIS memory at once.
A DataReader is fast.
A DataSet is slower than a DataReader.
A DataReader takes up few IIS and memory resources
but annexes the databaseconnection until it’s closed.
A DataSet takes up a lot more memory/IIS resources to
store all the data, but it doesn’t hold up a database connection until it’s
closed. The connection needs to be open only when Fill() is called.
A DataReader lasts as long as the connection to the
database is open. It can’t be persisted in a cookie or a session variable.
A DataSet lasts only until the page is reloaded
(posted back) unless it’s somehow persisted (for example, in a session
variable).
Fields in a DataReader are referenced by index number
or name.
You can reference fields in a DataSet by name, but
you must also name the DataTable and identify the row (index) that contains the
field.
A DataReader has no concept of primary keys,
constraints, views, or any other relational database management system (RDBMS)
concept except row and field.
A DataSet contains DataTables. A primary key may be
set for each DataTable, and relationships and constraints may be established
between them. DataViews may be created over the DataSet.
You can’t update a data source through a DataReader.
You can make changes to data in a DataSet and then
upload those changes back to the data source.
A DataReader connects to only onedata source.
A DataSet can be filled with Fill() from
multiple data sources.

No comments: