Thursday, June 11, 2009

Differences between DataSet and DataReader

Differences between DataSet and DataReader

The DataSet object is central to supporting disconnected, and distributed data scenarios with ADO.NET. The DataSet is a memory-resident representation of data that provides a consistent relational programming model regardless of the data source. It can be used with multiple and differing data sources, used with XML data, or used to manage data local to the application.

The DataSet represents a complete set of data including related tables, constraints, and relationships among the tables. I think this is something that gives the advantage upon the older ADO.

ADO.NET DataReader to retrieve a read-only, forward-only stream of data from a database. Using the DataReader can increase application performance and reduce system overhead because only one row at a time is ever in memory.

How do I know which one to use?

When deciding whether your application should use a DataReader (see Retrieving Data Using the DataReader) or a DataSet (see Creating and Using DataSets), you should consider the type of functionality that your application requires.

Use a DataSet to do the following:

Remote data between tiers or from an XML Web service. Interact with data dynamically such as binding to a Windows Forms control or combining and relating data from multiple sources. Cache data locally in your application. Provide a hierarchical XML view of relational data and use tools like an XSL Transformation or an XML Path Language (XPath) Query on your data.
For more information, see XML and the DataSet. Perform extensive processing on data without requiring an open connection to the data source, which frees the connection to be used by other clients.

If you do not require the functionality provided by the DataSet. You can improve the performance of your application by using the DataReader to return your data in a forward-only read-only fashion. Although the DataAdapter uses the DataReader to fill the contents of a DataSet (see Populating a DataSet from a DataAdapter), by using the DataReader you can receive performance gains because you will save memory that would be consumed by the DataSet, as well as saving the processing required to create and fill the contents of the DataSet.