DataSet, DataReader, and ADO. netdatareader In ado. NET

Source: Internet
Author: User

DataSet, DataReader, and ADO. netdatareader In ado. NET

ADO. NET provides two objects for retrieving relational data and storing it in the memory: DataSet and DataReader. DataSet provides a full set of relational data in the memory, including the relationships between tables, such as tables, order, and constraints. DataReader provides fast, forward-only, and read-only data streams from the database.

When using DataSet, DataAdapter (or CommandBuilder) is generally used to interact with the data source. DataView is used to sort and filter data in DataSet. DataSet can be inherited to create a DataSet of the reinforcement type. It is used to expose tables, rows, and columns as reinforcement type object attributes.
The following content includes when to use DataSet or DataReader, how to optimize access to the data they contain, and how to optimize the use of DataAdapter and DataView (including CommandBuilder ).


Comparison between DataSet and DataReader:
When designing an application, decide whether to use DataSet or DataReader to consider the features required by the application.

DataSet is used to implement the following functions of the application:
L multiple separated tables in the operation result.
2. Operate on data from multiple sources (such as hybrid data from multiple databases, XML files, and workbooks.
3. exchange data between layers or use XML Web Services. Unlike DataReader, DataSet can be passed to a remote client.
4. Repeat the same row set by buffering to improve performance (such as sorting, searching, or filtering data ).
5. A large amount of processing is performed on each line. Extended processing on the rows returned by using DataReader will lead to a longer connection time than necessary, thus reducing the efficiency.
6. use XML operations (such as XSLT conversion and Xpath query) to maintain data.

Use DataReader when the application requires the following features:
L no data buffering is required.
2. The result set being processed is too large to be fully stored in the memory.
3. You need to quickly access the data at one time and use the forward-only read-only method.
Note: When filling in DataSet, DataAdapter uses DataReader. Therefore, the performance obtained by using DataAdapter instead of DataSet is to save the memory consumed by DataSet and the cycle required to assemble DataSet. Most of the improvements to this performance are not real-name, so you should make the design decisions based on the required functions.

Benefits of using strong DataSet
Another advantage of using DataSet is that it can be inherited for creating a strongly Typed DataSet. Benefits of a strong DataSet include design-time check and Visual Studio. NET statement filling of a strong DataSet. When you fix the outline or link structure for the DataSet, you can create a strong DataSet and use rows and columns as the attributes of the object rather than the set of items. For example, you can expose the Name attribute of the Customer object instead of the column Name of a row in the exposed Customer table. Strong DataSet is derived from the DataSet class, so it does not sacrifice any functions of DataSet. That is to say, strong DataSet can also be remote and provided as a data source for data binding controls (such as DataGrid. If you do not know the outline, you can also benefit by using the common DataSet, but you lose the strong type.

Additional features of DataSet
Processing null values in a strongly Typed DataSet
When using a strongly typed DataSet, You can annotate the XML outline Definition Language (XSD) of DataSet to ensure that the strongly Typed DataSet correctly processes Null references. The nullValue annotation allows you to replace DBNull with a specific value of String. Empty, retain null reference, or generate an exception. Select the content that depends on the application. By default, an exception occurs when an empty reference is encountered.

Refresh data in DataSet
If you want to use the updated value to refresh the value in the dataset from the server, use DataAdapter. Fill. If the primary key is defined on the data table, DataAdapter. Fill matches the new row based on the primary key and changes the server data to an existing row. The RowState of the refreshed row is set to Unchanged, even if it is modified before refreshing. Note that if a primary key is defined for the data table, the new row of DataAdapter. Fill may duplicate the primary key value.
If you want to refresh a table with the current value of the server and keep the row in the table changing, you must first use DataAdapter. fill combines it, fills in a new data table, merges the data table (Merge) into a data set, and sets the value of preserveChanges to true.

Search data in DataSet
When you query rows that meet specific conditions in a dataset, you can use index-based tables to improve performance. When you specify a primary key value for a data table, an index is created. An index is also created when a data view (DataView) is created for a data table. The following are some tips for using index-based viewing:
If the query is performed on the primary key column of the data table, use able. Rows. Find to replace able. Select.
To query non-primary key columns, you can use the data view to speed up multiple data queries. When sorting is added to the data view, indexes used for search are created. The data view exposes the Find and FindRows methods for querying lower-layer data tables.
If you are not querying the sort view of a table, you can also create a data view for the data table to obtain the benefits of viewing the table based on indexes. Note that this is the only benefit if you execute multiple queries on the data. If you only execute a single query, the index creation process will reduce the performance by using the index.

Related Article

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.