Introduction to DataReader DataSet DataAdapter and DataView

  • 2020-05-07 20:20:28
  • OfStack

ADO.NET provides two objects for retrieving relational data and storing it in memory: DataSet and DataReader. DataSet provides a representation of relational data in memory -- a complete set of data that includes relationships between tables and sequences, constraints, and so on. DataReader provides a fast, forward-only, read-only flow of data from the database.

With DataSet, 1 generally USES DataAdapter (or perhaps CommandBuilder) to interact with the data source, and DataView to sort and filter the data in DataSet. DataSet can be inherited to create DataSet of the reinforced-type, which exposes tables, rows, and columns as object attributes of the reinforced-type.
The following sections include when to use DataSet or DataReader and how to optimize access to the data they contain, as well as how to optimize the use of DataAdapter and DataView (as well as CommandBuilder).
DataSet versus DataReader
When designing an application, deciding whether to use DataSet or DataReader takes into account the functionality the application requires.

USES DataSet to implement for the following functions of the application:
Multiple separate tables in the result of an l operation.
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, DataSet can be delivered to a remote client.
Improve performance by caching the same set of rows over and over again (such as sorting, searching, or filtering data).
5 perform a lot of processing per line. Extending processing on lines returned using DataReader reduces efficiency by making the connection last longer than necessary.
6 maintain data using XML operations, such as XSLT transforms and Xpath queries.

USES DataReader when the application requires the following functionality:
l does not require buffering data.
The result set being processed is too large to fit into memory.
3 the data needs to be accessed quickly once, in a forward-only, read-only manner.
Note: DataAdapter USES DataReader when filling DataSet. Therefore, the performance obtained by using DataAdapter instead of DataSet saves the memory consumed by DataSet and the cycles required to assemble DataSet. Most of this performance improvement is nominal, so you should make design decisions based on the functionality you need.

benefits of using strongly typed DataSet
Another benefit of using DataSet is that it can be inherited to create strongly typed DataSet. The benefits of strongly typed DataSet include design-time checking and strongly typed DataSet's Visual Studio.NET statement padding. When you have fixed an outline or relational structure for DataSet, you can create a strongly typed DataSet, using rows and columns as properties of objects rather than collections of items. For example, instead of exposing the column name of a row in the customer table, you can expose the Name attribute of an Customer object. Strongly typed DataSet is derived from the DataSet class and therefore does not sacrifice any functionality of DataSet, that is, strongly typed DataSet can also be remote and provided as a data source for data-bound controls such as DataGrid. If you don't know the outline, you can get the benefits of using the usual DataSet, but you lose the strong typing

Additional DataSet features
Null values are handled in strongly typed DataSet
When using strongly typed DataSet, you can annotate DataSet's XML outline definition language (XSD) to ensure that strongly typed DataSet correctly handles null (Null) references. The null value (nullValue) annotation allows you to replace DBNull with the specific value String.Empty, keep the null reference, or generate an exception. Select which of these depends on the content of the application, and by default an empty reference will generate an exception.

refreshes the data in DataSet
If you want to refresh the data set from the server with the updated values, use DataAdapter.Fill. If the primary key is defined on a data table, DataAdapter.Fill matches a new row based on the primary key and changes the server's data to an existing row. The RowState of the row being refreshed is set to Unchanged, even though it was modified before the refresh. Note that if a primary key is defined for the data table, DataAdapter.Fill adding a new row may duplicate the primary key value.
If you want to refresh a table with the current value of the server and keep the rows in the table changing, you must first combine it with DataAdapter.Fill, populate a new data table, then merge the data table (Merge) into a data set, and set preserveChanges to true.

searches for data in DataSet
Using index-based (index-based) view tables improves performance when querying rows that meet specific criteria in a dataset. When you specify a primary key (PrimaryKey) value for a data table, one index is created. Indexes are also created when creating a data view for the data table (DataView). Here are some tips for using index-based viewing:
If the query is on the primary key column of the data table, use DataTable.Rows.Find instead of DataTable.Select.
Querying non-primary key columns, you can use data views to speed up multiple data queries. When you add a sort to the data view, the index used in the search is established. The data view exposes the Find and FindRows methods for querying the underlying data table.
If you are not a sort view of the lookup table, you can also get the benefit of index-based table view by creating a data view for the table. Note that this is the only benefit if you execute multiple queries on the data. If you only execute a single query, the process of building an index will degrade performance by using the index.

data view (DataView) structure
When the data view is created, and when Sort, Ro
Use DataReader, DataSet, DataAdapter, and DataView
ADO.NET provides two objects for retrieving relational data and storing it in memory: DataSet and DataReader. DataSet provides a representation of relational data in memory -- a complete set of data that includes relationships between tables and orders, constraints, and so on. DataReader provides a fast, forward-only, read-only stream of data from the database.

With DataSet, 1 generally USES DataAdapter (or perhaps CommandBuilder) to interact with the data source, and DataView to sort and filter the data in DataSet. DataSet can be inherited to create DataSet of the strengthened type, which is used to expose tables, rows, and columns as object attributes of the strengthened type.

Related articles: