DataSet or DataReader?

原创 2004年10月16日 13:14:00
When I talk with working developers, the question I hear most often is: Which is the right approach? The answer depends on your problem domain. The DataReader is a forward-only fire hose for data. It is often the perfect solution for reporting on data in the database. But forcing a design to use it may involve writing your own DataSet-like container for data if you need to use the data after you read a single row.

The DataSet, on the other hand, is a great way to hold data from the database, but filling it comes with a performance penalty. If you need to manipulate your data and save it back to the database, the DataSet is really the only game in town. Using the DataSet to simply report on data (like on a Web page, for instance) usually is the wrong approach. I say "usually" because there are always exceptions to this rule.

Let's look at a specific domain problem: an e-commerce site. Assuming we have a large number of salable items, I would use DataReaders to show the user the products they are looking for and a DataSet for the shopping basket. When you list products by caching all the items in your catalog, you usually lose efficiency when the cache is too large. I usually like to let the database handle the caching of recently used items. It does a better job in most cases. But a smaller DataSet to contain the user's information and shopping basket could be cached in the Web session so that we can limit the number of database reads and writes. If the visitor never ends up buying anything, their shopping basket could simply disappear (and never get persisted) when his or her session dies.

Just remember this caveat: If you need it cached, use a DataSet; if you need it once, use a DataReader.

DataSet,DataTable,DataReader,DataAdapter 区别

简介: DataSet:表示一个数据集,是数据在内存中的缓存,可以包括多个表。         DataSet.Table(“表名”).Rows(行号)(“字段名”)   DataTable:...
  • X_X_OO
  • X_X_OO
  • 2016年09月13日 20:11
  • 407

dataset datareader区别

  • ansontang
  • ansontang
  • 2008年03月05日 20:46
  • 395


作者:Jonathan Goodyear        出处:网络 ...
  • 21aspnet
  • 21aspnet
  • 2004年10月25日 19:13
  • 2720


  • nileel
  • nileel
  • 2007年06月05日 17:15
  • 2456


public static DataSet ConvertDataReaderToDataSet(SqlDataReader reader) { DataSet dataSet = new DataS...
  • windok2004
  • windok2004
  • 2008年04月04日 09:24
  • 3961


SqlDataReader通常和SqlCommand一同使用,常用语简单浏览并且耗时较短的数据库操作。 DataSet长与SqlDataAdapter一同使用,DataSet会将所需数据...
  • huyuyang6688
  • huyuyang6688
  • 2014年09月23日 11:45
  • 3701


ADO.NET2.0提供了两个用于检索关系数据的对象: DataSet和DataReader。并且这两个对象都可以将检索的关系数据存储在内存中。在软件开发过程中经常用到这两个控件,由于这两个控件在使...
  • jyz123456
  • jyz123456
  • 2012年04月27日 10:17
  • 452


#region -----------------------SQL数据库的操作方法,返回DataReader,DataSet,DataTable等对象--------------------- ...
  • lijingrong_ljr
  • lijingrong_ljr
  • 2015年08月07日 15:25
  • 538


先看一下视频中讲的这章的框架:        在这里重点讲述一下DataReader 和DataSet。      一,DataReader:     1,简单介绍:DataReade...
  • liujiahan629629
  • liujiahan629629
  • 2013年01月26日 15:00
  • 5362
您举报文章:DataSet or DataReader?