Difference between datatable, dataview and Dataset

Source: Internet
Author: User

1. dataview indicates the datatable. The records in the able table have no sequence, but the display can be in different order (dataview ),

But it is still the same table, so a datatable can have multiple dataviews. By default, the datatable is accessed.

Defaultview. Dataset is a set of datatable, and there can be more than one datatable

2. dataview is a virtual view of a able. It is mainly used to display data. In fact, all data changes occur in the datatable.

For example, in a database, dataset is a simple database, a set of multiple tables (datatable), and a datatable is the corresponding number.

Tables in the database, while dataview corresponds to the view in the database ).

 

 

Ado. Net has an object used to create an abstract model of any data source. These include dataset, datatable, datarow, dataview, and datarelation.

All these objects are defined in the system. Data namespace. They form an abstract model so that the same programming interface can be used for both Windows form, web form, and Web Service programming.

In practical applications, most of these objects operate on data in relational databases such as SQL Server. However, they can process various types of data regardless of their physical storage media.

You can use a DataSet object to package and associate data in each table, and use the able class to Process Table-type data, while the datarow object can process data in a row in a table.

All three objects are packaged with different logical aggregation layers. Dataset is a combination of datatable and others. Datatable is a combination of datarow and others. Datarow is a combination of fields and other fields. However, these objects do not have built-in filtering and sorting functions.

ADO. NET provides some classes to handle important aspects of this database application. In. Net beta2, the two most important objects are dataview and dataviewmanager.

Note: dataviewmanager is unique in beta2. In beta1, the corresponding functions are completed by datasetview.

Custom Data View

The dataview class is used to represent the views of a custom datatable. The relationship between able and dataview follows the famous design pattern-document/view pattern, where datatable is a document and dataview is a view.

At any time, you can have multiple views based on the same data. More importantly, you can process each view with its own set of attributes, methods, and events as an independent object. This also represents a huge leap over ADO.

Ado recordset can define filter strings. Once you set up this character, only data that matches a specific standard can be read and written. The filter attribute works similarly to the dynamic where clause. It simply hides some records in the same recordset object view.

In ADO, you never have an independent view object. A filtered recordset is always the same object, except that the number of records displayed is smaller than the actual number of records.

If you do not need to process different views at the same time, the above problem does not matter. The programming interface provides A recordset function that can be a table or a view. However, this cannot happen at the same time during creation. At a specific time point, the recordset can only be a table without a filter string or a view with a filter string activated.

The clone of recordset provides a better solution to this structure restriction. As clonation and the case of table Dolly, as described in Part 1, cloning A recordset is less costly because it does not copy data, but only copies the basic structure of A recordset. To process two or more views of the same data, you can use two or more clones to separate a set of filter strings.

Figure 1 process different views of the same recordset In ADO

In ADO. net, you can use the dataview object provided by the new object model. The dataview object of ADO. NET is used to represent a custom view of a given data table, but you can process it like a separate object. The dataview object retains a reference to the table and allows updates to it.

Figure 2 operations on different views of the same data table in ADO. net

In terms of function, using ADO recordset To clone and using special view objects is the same function, allowing you to filter and operate the selected data rows, and process multiple views at the same time.

In-depth dataview object

The dataview object inherits the marshalbyvaluecomponent and implements a set of interfaces to make it available in the data binding control. Public class dataview

Inherits extends albyvaluecomponent

Implements ibindinglist, ilist, icollection, ienumerable ,_

Itypedlist, isupportinitialize

The class derived from marshalbyvaluecomponent is A. NET remote component. You can use values to set columns, that is, serialize objects to the target application domain. (For more information about the. NET Component, see the following section)

Content in dataview can be operated through many programming interfaces, including collections, lists, and enumerators. The ibindinglist interface ensures that this class provides all the necessary features to support complex and simple data binding.

In general, the dataview object can be used for two purposes. First, the view is very important for the datasource field in the associated able object and data binding control. Second, it also provides a layer of packaging for the connected datatable, allowing you to filter, sort, edit, and browse.

Dataview is not the only data driver class that can be remotely operated by passing values. Dataset and datatable have the same capabilities, especially in interoperability scenarios.

Create dataviewpublic dataview ();

Public dataview (datatable );

Dataview is available only when it is connected to an existing, possibly non-empty able object. Generally, this connection is specified during construction.

Dataview DV;

DV = new dataview (thedataset. Tables ["employees"]);

However, you can create a new view and associate it with the table by using the table attribute.

Dataview DV = new dataview ();

DV. Table = thedataset. Tables ["employees"];

The dataview constructor allows you to get a dataview object from the able. If needed, or vice versa. In fact, the defaultview attribute of the datatable object returns a dataview object for the table.

Dataview DV = DT. defaultview;

Once you have a dataview object, you can use its attributes to create a data row set that you want users to see. Generally, you can use the following attributes:

Rowfilter

Sort

The former allows you to customize rules for matching visible data in the view. The latter uses expressions for sorting. Of course, you can use any combination of the two.

Rowfilter is a read/write attribute used to read and set table filter expressions.

Public Virtual string rowfilter {Get; set ;}

You can use any legal combination of column names, logical and numeric operators, and constants to form an expression. The following are examples: DV. rowfilter = "Country = 'usa '";

DV. rowfilter = "employeeid> 5 and birthdate <#1/31/82 #"

DV. rowfilter = "description like '* product *'"

Let's take a look at the basic filter rules and operators.

The filter string is the logical connection of the expression. You can use and, or, not to connect to a short expression, or you can use parentheses to form a clause to specify a priority operation.

The clauses that usually contain column names are compared with letters, numbers, dates, or other column names. Here, you can use Relational operators and arithmetic operators, such as >=, <,>, +, *, % (Modulo), and so on.

If the row to be selected cannot be easily expressed by arithmetic or logical operators, you can use the in operator. The following code selects a random row:

DV. rowfilter = "employeeid in (2, 4, 5 )"

You can also use wildcards * and %, which are more useful when used together with the like operator. They all represent any number of characters and can be used with each other.

Note that if the like clause already contains * or % characters, you must enclose it in square brackets to avoid ambiguity. If, unfortunately, the Chinese brackets of the string also exist, they must also be enclosed. In this way, the matching statement is as follows:

DV. rowfilter = "description like '[[] * [] product [[] * []"

Wildcard characters can only be used at the beginning or end of a filter string, but cannot appear in the middle of a string. For example, the following statement produces a runtime error:

DV. rowfilter = "description like 'prod * CT"

The string must start with single quotes, and the date type must start with the # symbol. Numeric values can use decimal points and scientific notation.

Rowfilter also supports aggregate functions, such as sum, Count, Min, Max, and AVG. If there are no data rows in the table, the function returns NULL.

At the end of introducing the rowfilter expression, let's discuss three convenient functions: Len, IIF, and substring.

As its name, Len () returns the length of a specific expression. This expression can be a column name or another legal expression.

Substring () returns the character substring of a specified expression starting from a specific position.

I like IIF () most, which has one or two values according to the logical expression value. IIF is a compact expression of the if-then-else statement. Syntax:

IIF (expression, if_true, if_false)

Through this function, you can create a very complex filter string. For example, assume that you obtain the Employees table from the SQL server's northwind database, the following expressions can be used to select employees whose employeeid is less than 6 and whose lastname is an even number of characters and whose employeeid is greater than 6 and whose lastname is an odd number of characters.

IIF (employeeid <6, Len (lastname) % 2 = 0, Len (lastname) % 2> 0)

Result displayed (the sample application will be discussed later)

Figure 3 filter the tables in northwind

The example program is a Windows form application, which uses two DataGrid

Control to implement the master/detail structure. A grid is generated when it is loaded, that is

After the server data adapter completes Data Reading. Please note that data

The adapter is introduced in Beta 2 and corresponds to the sqldatasetcommand class in Beta 1.

Pre-arranged View

In the preceding example, the DataGrid must be used to pre-arrange data rows in the view to refresh the user interface. The automatic mechanism is. net.

The product of data binding. The DataGrid is a data binding control that obtains data through the datasource attribute. Dataview is a data binding class that can be used to construct the content of the datasource attribute.

What should you do if you want to use another control other than the DataGrid? What if you don't want to use automatic data binding? How should we pre-arrange the selected data rows in the view?

The advantage of dataview is that multiple views can be defined for a single able. When two DataGrid needs to display data in the same datatable, two dataviews can be defined to bind to the control.

 

Datareader is used to read data from one row.
Dataset is a dataset... it contains multiple data tables. used to save the queried data. compared with datareader, datareader can create local copies and perform many operations, such as filtering and sorting. If data is not operated, it is best to select datareader. dataset is a class.

DataGrid is the control for displaying data.
Datatable, a data table

Regard dataset as a database,
Think of datatable as a table in the database
View the datagridview as a table tool for displaying data in the table.

 

Datareader is based on a connection. The data returned by datareader is read-only and forward-oriented. It is suitable for simple browsing and time-consuming operations. The DataSet object reads the required data into the memory and then disconnects. It is suitable for complex and long-time operations on data and data needs to be updated.

 

Sqldatareader only establishes a pointer relationship with the database. It does not read any data from the data before calling the read () method, but calls read () only one piece of data is read from the database. A able is a copy of a data table. As for dataset, it can be regarded as a database because it can contain not only multiple tables but also the relationship between tables. therefore, sqldatareader is recommended for massive data access (especially paging access), which can save a lot of memory overhead.

Content source:

Http://blog.csdn.net/hdhai9451/article/details/4000513

Http://wenku.baidu.com/view/78d9c3373968011ca30091eb.html

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.