DataSet vs. DataReader
DataReader
- A dataReader is specific to a data provider(for example, SqlDataReader, OdbcDataReader, and OleDbDataReader).
- The data retrieved through a DataReader is read-only.
- 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.
- A DataReader presents data through a direct connection to the data source. Only one row of data is stored in memory at any one time.
- A DataReader takes up few IIS and memory resources but annexes the database connection until it’s closed.
- 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.
- Columns in a DataReader are referenced by index or name.
- A DataReader has no concept of primary keys, constraints, views, or any other relational database management system concepts, except rows and columns.
- You can’t update a data source through a DataReader.
- A DataReader connects to only one data source
DataSet
- 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(for example, SqlDataAdapter, OdbcDataAdapter and OleDbDataAdapter).
- The data retrieved through a DataSet is read-write.
- You can work with data in a DataSet in any order you choose as many times as you like.
- A DataSet stores all the data from the data source in memory at once.
- A DataSet takes up a lot more IIS and memory 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 DataSet lasts only until the page is reloaded(posted back), unless it’s somehow persisted(for example, in a session varriable).
- You can reference columns in a DataSet by name, but you must also name the DataTable and identify the row(index) that contains the column.
- A DataSet contains a collection of DataTable objects. A primary key may be set for each DataTable, and relationships and constraints may be established between them.
- You can make changes to data in a DataSet, and then send those changes back to the data source.
- A DataSet can be filled with Fill( ) from multiple data sources but, once the data is retrieved, is not connected to any of them.