DataSet or DataReader class

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
How do I know which one to use?

Hi,

It's amazing how many people simply use one or another without considering do they need dataset or datareader data retrieval class before they start to code ... oftentimes making mistakes that may have affected on performance of the application ... Ok, this is an article written by Mark A. Strawmyer that will help you understand when should be used certain class



How do I know which one to use?


You can use the following information to help you decide which data retrieval is right for your purposes.

When to consider using a DataReader:

  • The DataReader is a better choice for applications that require optimized read-only and forward-only access to data such as binding to a DataGrid control. The sooner the data is off-loaded from the DataReader and the connection is closed the better the application performance.
  • The DataReader is a better choice when you are planning to make repeated calls to retrieve small amounts of information or the data you are retrieving must be as up to date as possible each time it is used.

When to consider using a DataSet:

  • The DataSet is a better choice for applications that will not off-load the query result immediately, or when there is extensive processing such as complex business logic involved between data accesses. The DataSet will retrieve the data, off-load the data into memory and return the database connection to the connection pool, where as a DataReader would keep the connection locked open until processing is complete. This could easily cause a high traffic application to run out of available database connections.
  • The DataSet is a better choice when you need to navigate through the data more than once. For example, if you have multiple controls you need to build off the same data, then a DataSet is the better answer because a DataReader can only be read once so it can only be bound to a single control and would require the data to be retrieved for use with each control.
  • The DataSet is a better choice when the data does not change frequently enough to warrant always retrieving it from the database or is specific to the user requesting the data. A DataSet can be stored in Session or Application variables or cached through the System.Web.Caching.Cache class to improve application performance by not having to retrieve the data from the database each time it is needed.
  • The DataSet is a better choice when building a Web service that will return the retrieved data. Since a DataSet is serializable it can serve as the return value. Since a DataReader requires a persistent database connection, it cannot be used as a return type from a Web service.

It is important to remember there are now two data retrieval classes. Each was created for a purpose that is distinct from the other. Consider your options carefully before you make your decision.


Kind regards ;)
 
Last edited:
Back
Top