Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
more resources

Use DataReader or DataSet?
ADO.NET exposes two primary classes for retrieving data. Learn to decide which one to use.
by Jonathan Goodyear, MCSD, MCP, CLS

Posted February 28, 2003

ADVERTISEMENT
I hear this question all the time: "Should I use the DataReader or DataSet class for my ASP.NET Web applications?" The most common misconception I've seen posted in articles and newsgroups states that DataReader (short for either SqlDataReader or OleDbDataReader) is better than DataSet. Sometimes I see it mentioned the other way around. The truth is that Microsoft created these data-access classes because they are both needed. Each has unique strengths and weaknesses, and their usage should be determined by the situation you are facing.

This article sheds some light on the issue by providing you with some guidelines for when to use the DataReader class and when to use the DataSet class, in the context of ASP.NET. The rules might change in the context of a client-based Windows Forms application. I am assuming that you have used, and are familiar with, both the DataReader and DataSet classes.

Use the DataReader Class
These are the ideal situations when you should use the DataReader class:

  • The data that you are retrieving must always be fresh, and therefore must be retrieved from the database each time you need it. There is less overhead associated with creating a DataReader class, and performance over DataSet improves rapidly with increasing load (see the Visual Studio Magazine articles in Resources).
  • You have simple needs for each row of data. The best example of this would be simply binding DataReader to a Web control, such as DataGrid or DropDownList.
  • You simply need forward-only, read-only access to XML data from a database. In this case, you can use the ExecuteXmlReader() method of the SQLCommand object to obtain an XmlReader class (the XML equivalent of DataReader). This requires a SQL Server query using the FOR XML clause, or an ntext field that contains valid XML.
  • You plan to make several repeated calls to the database to retrieve small pieces of information. The performance data referred to in the first bullet applies to an even greater scale here.
  • It is true that many of the features that make DataSet classes great, such as the ability to set up relations between tables, are better left to client-based Windows Forms applications. There are numerous occasions, however, when DataSet classes are preferable to DataReader classes, and a few where you cannot use DataReader classes at all.
Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home