Introduction to DataReader, Datasets, DataAdapter, and DataView _c# tutorials

Source: Internet
Author: User
Ado. NET provides two objects for retrieving relational data and storing it in memory, respectively, DataSet and DataReader. A dataset provides the performance of relational data in memory-a complete collection of data, including tables and orders, constraints, and relationships between tables. DataReader provides fast, forward-only, read-only data streams from the database.

When using a dataset, you typically use DataAdapter (or possibly CommandBuilder) to interact with the data source, sorting and filtering the data in the DataSet with DataView. The dataset can be inherited to create a dataset of the hardening type that exposes the table, row, and column as the object attribute of the hardening type.
The following content includes when to use Datasets or DataReader, and how to optimize access to the data they contain, as well as how to optimize the use of DataAdapter and DataView (also including CommandBuilder).
Comparison of DataSet and DataReader
When designing an application, deciding whether to use a dataset or DataReader requires consideration of the functionality required by your application.

the dataset is used to implement the following functions of the application
L multiple detached tables in the result of the operation.
2 manipulate data from multiple sources, such as mixed data from multiple databases, XML files, and spreadsheets.
3 Exchange data between layers or use XML Web services. Unlike DataReader, a dataset can be passed to a remote client.
4 Use the same rowset repeatedly to improve performance (such as sorting, searching, or filtering data) by Buffering.
5 Perform a large amount of processing per line. Extending processing on rows returned using DataReader will make the connection longer than necessary to reduce efficiency.
6 Maintain data using XML operations such as XSLT transformations and XPath queries.

use DataReader when your application requires the following features
L Do not need buffered data.
2 The result set being processed is too large to be fully placed in memory.
3 requires a quick, one-time access to the data, using a forward-only read-only approach.
Note: When populating a DataSet, DataAdapter uses DataReader. Thus, the performance of using DataAdapter instead of a dataset saves the memory that the dataset consumes and the cycles required to assemble the dataset. Most of this performance improvement is nominal, so you should make design decisions based on the functionality you need.

benefits of using a strongly typed dataset
Another benefit of using a dataset is that it can be inherited to create a strongly typed DataSet. The benefits of a strongly typed dataset include design-time checks and visual Studio for strongly typed datasets. NET statement fill. When you fix an outline or relational structure for a dataset, you can create a strongly typed DataSet, which takes rows and columns as attributes of an object rather than as a collection of items. For example, you can expose the Name property of the customer object as a substitute for the column names that expose a row of the Customers table. A strongly typed DataSet is derived from the DataSet class and therefore does not sacrifice any functionality of the dataset, that is, a strongly typed dataset can also be remote and provided as a data source for a data-bound control such as a DataGrid. If you don't know the outline, you can benefit by using the usual dataset, but you lose the strong type

additional attributes of the dataset
Handling null values in a strongly typed dataset
When using a strongly typed DataSet, you can annotate the dataset's XML Outline definition language (XSD) to ensure that the strongly typed dataset correctly handles null (NULL) references. A null value (nullvalue) annotation enables you to replace DBNull with a specific value of String.Empty, keep a null reference, or produce an exception. When you choose which of these depends on the content of your application, an exception is generated by default when a null reference is encountered.

refreshing data in a dataset
Use DataAdapter.Fill if you want to refresh the values in the dataset from the server using the updated values. If the primary key is defined on the datasheet, DataAdapter.Fill matches the new row based on the primary key and changes the server's data to the existing row. The RowState of the refreshed row is set to unchanged, even though it has been modified before the refresh. Note If you define a primary key for a datasheet, DataAdapter.Fill adding a new row may duplicate the primary key value.
If you want to refresh a table with the server's current value and keep the rows in the table changed, You must first choose to use DataAdapter.Fill to combine it, populate a new datasheet, then merge the data table (merge) into a dataset and set the PreserveChanges value to true.

searching for data in a dataset
Using an indexed (index-based) View table improves performance when querying rows that meet specific criteria in a dataset. When you assign a primary key (PrimaryKey) value to a datasheet, an index is established. Indexing is also established when a Data View (DataView) is established for a datasheet. Here are some tips for using indexed viewing:
If the query is on the primary key column of the datasheet, use DataTable.Rows.Find instead of datatable.select.
Query for non-primary key columns, you can use a data view to increase the speed of multiple data queries. When you add a sort to a data view, the index used for the search is established. The Data view exposes the find and FindRows methods for querying the underlying data tables.
If you are not a sort view of a query table, you can also gain an indexed view table by creating a data view for the datasheet. Note that if you execute multiple queries on the data, this is the only benefit. If you are only performing a single query, the process of indexing will degrade performance by using indexes.

Data View (DATAVIEW) Structure
When the Data view is established, and when sort, Ro
Using DataReader, Datasets, DataAdapter, and DataView
Ado. NET provides two objects for retrieving relational data and storing it in memory, respectively, DataSet and DataReader. A dataset provides the performance of relational data in memory-a complete collection of data, including tables and orders, constraints, and relationships between tables. DataReader provides fast, forward-only, read-only data streams from the database.

When using a dataset, you typically use DataAdapter (or possibly CommandBuilder) to interact with the data source, sorting and filtering the data in the DataSet with DataView. The dataset can be inherited to create a dataset of the hardening type that exposes the table, row, and column as the object attribute of the hardening type.

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.