Programming Index Cards

July 23, 2008

DataSet vs. DataReader

Filed under: ADO.Net, database, programming — apcig @ 1:24 pm

DataSet vs. DataReader1

1. DataReaders have less overhead than DataSets and are more efficient.
2. DataReaders can be databound (i.e. to a DataGrid).
3. Iterated once through

but

1. DataSets are disconnected. They are like disconnected models of the database.
2. DataSets can be made into Session objects or files, for persistence
3. DataSets can provide access to more than one table and table relationships
4. DataSets can be bound to multiple controls
5. Reiterated through multiple times.
5. You can jump to a particular record or go backwards through a DataSet.
6. Ability to name your table

TABLE 6.3: COMPARING DATAREADERS AND DATASETS2
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.

1 My Library Syndication

2 Beginning ASP.NET 1.1 Databases: From Novice to Professional

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.