Use DataReader, DataSet, DataAdapter and DataView

zhaozj2021-02-16  81

ADO.NET provides two objects to retrieve relational data and store it in memory, named DataSet and DataReader. DataSet provides performance in memory-related data - including complete data sets of relationships between tables and order, constraints. DataReader provides fast, forward, read-only data streams from the database.

When using DataSet, DataAPter is usually interacting with data source using DataAPter, sorting and filtering data in DataSet with dataView. DataSet can be inherited to establish a strengthening type DataSet for exposure table, row, column as an intensive type object property.

The following content contains when using DataSet or DataReader, and how to optimize the data contained in them, and how to optimize the use of DataAdapter and DataView (also including CommandBuilder).

DataSet and DataReader comparison

When designing an application, decide to use DataSet or DataReader to consider the features you need.

Using DataSet is to implement the following functions to implement applications:

l Multiple separated tables in the operation result.

l Operate data from multiple sources (eg, mixed data from multiple databases, XML files, and spreadsheets).

l Exchange data between layers or uses an XML web service. Unlike DataRead, DataSet can be passed to the remote client.

l Use the same row collection by buffering to improve performance (such as sort, search, or filtering data).

l Perform a lot of processing per line. The extended processing on the row returned to DataReader will make the connection existence than the necessary longer, thereby reducing efficiency.

l Maintain data using XML operations (such as XSLT conversion and XPath query).

Use DataReader when the application needs the following functions:

l Don't buffer data.

l The result set is being processed is too large to place all in memory.

l You need to access data from time to one-time and use only ways only to read.

Note: When filling DataSet, DataAdapter uses DataReader. So using DataAdapter instead of DataSet's performance is to save the cycle required by DataSet consumption and assembly DataSet. This performance is raised, so you should do design decisions based on the functions needed.

Benefits using strong type DataSet

Another benefit using DataSet is that it can be inherited to create a strong type of DataSet. Strong Type DataSet is the benefits of designing checks and stronger Type Dataset's Visual Studio .NET statement population. When you secure the outline or relational structure for the DataSet, you can build strong type DataSet, line and column as a collection of properties of the object rather than items. For example, as a replacement of the column name of a certain line of exposure to the customer table, you can expose the Name property of the Customer object. Strong Type DataSet is derived from the DataSet class, so no feature of DataSet is not sacrificing, that is, strong type DataSet can also be remote and provided as a data source for data binding controls (eg, DataGrid). If you don't know the outline, you can also get the benefits of using the usual DataSet, but loses the additional characteristics of strong type DataSet.

Handling null value in strong type DataSet

When using strong Type Dataset, you can give a DataSet's XML Outline Definition Language (XSD) to ensure that strong type Dataset correctly handles null (NULL) references. NULLVALUE Note Allows you to replace DBNULL with String.empty, maintain an empty reference, or an exception. Select which dependent content of the application, by default, an empty reference will produce an exception. Refresh the data in DataSet

If you want to use the updated value from the server to refresh the value in the data set, use DataAdapter.Fill. If the primary key is defined on the data table, DataAdapter.Fill is based on the primary key to match the new row and convert the data of the server to the existing row. The rowstate of the brush is set to Unchanged, even before it is refreshed. Note If the primary key is defined for the data table, DataAPter.Fill adds a new row may repeat the main key value.

If you want to refresh a table with the current value of the server, keep the table's change in the table, you must prefer to combine it, populate a new data sheet, then combine the data table and enter a data set, and Set the PreserveChanges value to true.

Search data in DataSet

Using an index-based view table will increase performance when querying rows that match specific conditions. When you specify a primarykey value, an index is established when you specify a primary key. An index is also established when establishing a data view (DataView) for the data table. Here are some techniques that use index-based conditions:

If the query is performed on the primary key column of the data table, use DataTable.Rows.Find instead of DataTable.select.

Query non-main keys, you can use the data view to increase the speed of multiple data queries. When you add a sort to the data view, the index used when searching is established. The data view exposes the Find and Findrows methods for query the underlying data table.

If you are not a sorting view of the query table, you can also get the benefits of an index-based viewing table by building a data view for the data table. Note that if you perform multiple queries on your data, this is the only benefit. If you only perform a single query, you need to build an index will reduce performance because of the use of indexes.

Data View (DataView) structure

When the data view is established, and when the sort, rowfilter or rowstatefilter or attribute is modified, the data view is the data set index in the lower data table. When establishing a data view object, use the data view constructor of the Sort, RowFilter, and RowStateFilter values ​​as the parameters. The result is a set of indexes. Create a "empty" data view, then set the sort, rowfilter, and rowstatefilter properties will result in at least two indexes.

转载请注明原文地址:https://www.9cbs.com/read-12464.html

New Post(0)