What’s better: DataSet or DataReader?

Technology CommunityCategory: ADO.NETWhat’s better: DataSet or DataReader?
VietMX Staff asked 3 years ago

It depends on your needs. One of the most important differences is that a DataReader will retain an open connection to your database until you’re done with it while a DataSet will be an in-memory object. If you bind a control to a DataReader then it’s still open. In addition, a DataReader is a forward only approach to reading data that can’t be manipulated. With a DataSet you can move back and forth and manipulate the data as you see fit.

Some additional features: DataSets can be serialized and represented in XML and, therefore, easily passed around to other tiers. DataReaders can’t be serialized.

On the other hand if you have a large amount of rows to read from the database that you hand off to some process for a business rule a DataReader may make more sense rather than loading a DataSet with all the rows, taking up memory and possibly affecting scalability.