Difference between datareader and Dataset

Source: Internet
Author: User

Explanation 1

Datareader And Dataset The biggest difference is that , Datareader Always in use Sqlconnection (commonly known as non-disconnected connection ), When you operate databases online, Any pair Sqlconnection Will cause Datareader Exception . Because Datareader Load only one piece of data in the memory at a time , Therefore, the occupied memory is very small. Because Datareader Special and high performance, So Datareader Yes, After reading the first article, you cannot read the first article.
Dataset The data is loaded into the memory at a time, Abandon database connections (commonly known as disconnected connections ). Once read, the database connection is abandoned, Because Dataset Load all the data into the memory, Therefore, memory consumption is relatively high. However Datareader Flexible, Dynamically Add rows , Column , Data, Return and update databases .


Second explanation

Both datareader and dataset can read data from the data source. Datareader itself is built through the idbcommand. executereader () method, while dataset is filled by the dbdataadapter. Fill () method. In addition, the two work in a significantly different way: the execution process of datareader cannot be separated from the database connection, that is, idbconnection cannot be used when datareader reads data. close () method to close the database connection. When using dataset to obtain data, you can disconnect the database because dbdataadapter is already responsible for retrieving data to the application server.
Because of these differencesProgramSpecial attention is required. For example, after you use datareader to obtain data, you should take the initiative to close the database connection; otherwise, an exception may occur when the database connection pool overflows.


Third explanation

You can use the datareader class objects or dataset class objects to read data from the database. However, they are different and generally include the following:

1.DatareaderIs a data management provider class, while dataset is a general data class.

2.DatasetThe data to be retrieved must be filled by the bridge dataadapter. Since the datareader itself is the management provider, it can useExecutereader ()Method to obtain data.

3.DatareaderIt is online processing. When the connection is closed, data cannot be read. dataset can process data offline and copy data from the database to local storage, when the connection is closed, you can still process data in dataset, or even add restrictions to tables stored locally.

4.DatareaderData can only be read forward, but data cannot be modified. dataset can read rows in any order, search, sort, and filter rows in a flexible manner, or even change these rows, then synchronize these changes to the database.

5.Reading data from datareader is faster than dataset.

6.Because dataset is offline, dataset cannot be used to lock the database during transaction processing. Because when the dataset is filled, the connection to the database is automatically disconnected, and the database cannot be locked.


It can be seen that in general cases, dataset and datareader may replace each other. When there are specific requirements, you need to carefully analyze which method is more appropriate.

DatareaderThe most fundamental difference from dataset lies in online processing and offline processing. When you are online, the real data of the database is obtained. If you are always online, the network communication burden is increased. Offline data is copied locally, which reduces the network burden and makes it easier for programs to process data. If the offline time is too long, the data you see may not be real data.

Additional experience:Dataset and datareader are used to mix large data volumes. When the data volume in a single table is large, it is unwise to use dataset because dataset stores data in the form of able memory, put large data into the memory at a time, which is very memory-consuming. Compared with dataset, datareader is much more elegant. It reads only one piece of data at a time and then calls the mechanism cyclically. But it also has its drawbacks, that is, it is relatively non-disconnected, but it is advantageous for memory consumption. Dataset also has its own advantages in most application scenarios, such as full decoupling, one operation, and domain model operation.

Therefore, we need to use these two objects reasonably based on different scenarios.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.