DataTable, DataView, Dataset differences

Source: Internet
Author: User

A classic answer to the differences between DataTable, DataView, and Dataset

1, DataVIew is the expression of the DataTable, DataTable table records are not sequential, but the display can have a different order (DATAVIEW),

But still the same table, so a DataTable can have multiple DataView, the default access to the DataTable, is actually to visit his

DefaultView. A DataSet is a collection of DataTable, or it can have more than one DataTable

2, DataView is a virtual view of the DataTable, mainly used to display data, in fact, the data changes are occurring in the DataTable.

If you use a database analogy, a dataset is a simple database, a collection of multiple tables (DataTable), a DataTable is the corresponding number

The DataView corresponds to the view in the database, according to the table in the library.

Ado. NET has a layer of objects that are used to create an abstract model of any data source. These include dataset,datatable,datarow,dataview,datarelation and so on.

All these objects are defined in the System.Data namespace. They form an abstract model that allows the same programming interface to be used regardless of whether the Windows Form,web form or WEB service is programmed. In practical applications, most of these objects operate on data in relational databases such as SQL Server. However, they can handle a variety of data regardless of its physical storage medium. You can use a DataSet object to package and correlate data from tables, use a DataTable class to work with tabular data, and a DataRow object to manipulate the data of a row in a table. All three objects are packaged with data, but have different levels of logical aggregation. Datasets are a combination of DataTable and other. And the DataTable is a DataRow and other combinations. A DataRow is a combination of fields and other. However, none of these objects have built-in filtering and sorting capabilities. Ado. NET provides classes to handle the important aspects of this database application. In. Net Beta2, the two most important objects in this area are DataView and DataViewManager. Note: DataViewManager is unique in Beta2. In Beta1, the corresponding function is completed by DataSetView.

Custom Data View the DataView class is used to represent a view of a custom DataTable. The relationship between DataTable and DataView follows a well-known design pattern-document/view mode, in which a DataTable is a document and DataView is a view. At any time, you can have multiple different views based on the same data. More importantly, you can handle each view that has its own set of properties, methods, and events as a separate object. This also represents a huge leap forward relative to ADO. The ADO recordset can define a filter string. Once you have established this character channeling, only data that matches a specific standard can be read and written. The Filter property works much like a dynamic where clause. It simply hides some records on the same Recordset object view. In ADO, you never have a separate view object. A filtered recordset is always the same object, except that it shows less than the actual number of records. If you don't need to work on some different views at the same time, the above question doesn't matter. The programming interface gives the recordset the ability to be either a table or a view. However, this cannot occur at the same time when it is created. At a particular point, the Recordset can only be a table with no strings filtered or a view that activates a filtered string. The cloning of a Recordset provides a good way to solve this structural limitation. As Clonation and the case of Table Dolly, in Part 1, the clone Recordset is relatively inexpensive because it does not replicate data, but simply duplicates the basic structure of the recordset. To work with two or more two views of the same data, you can use two or more clones, each with a corresponding filter string.

Figure one working with different views of the same recordset in ADO at ADO, you can use the DataView object provided by the new object model. Ado. NET DataView object is used to represent a customized view of a given data table, but you can handle it as if it were a separate object. The DataView object retains a reference to the table and allows it to be updated.

Figure II Operation of different views of the same data table in ADO, using ADO recordset cloning and using special view objects to accomplish the same function, is to allow you to implement filtering, the selected data rows to operate, and simultaneously handle multiple views.

In-depth DataView objects DataView objects inherit marshalbyvaluecomponent and implement a set of interfaces that make them available in a data-bound control. Public Class DataView Inherits marshalbyvaluecomponent Implements IBindingList, IList, icollection,ienumerable, _ ITypedList, ISupportInitialize

A class derived from MarshalByValueComponent is a. NET remote component that can be set by value-that is, serializing the object to the target application domain. (See below for more information.) NET components) DataView content can be manipulated through a number of programming interfaces, including collections, lists, and enumerators. The IBindingList interface ensures that the class provides all the necessary features to support complex and simple data binding. In general, DataView objects can be used to achieve two purposes. First, the view is important for associating DataSource fields in DataTable objects and data-bound controls. Second, it also provides a layer of packaging for connected DataTable, allowing you to filter, sort, edit and browse. DataView is not the only data-driven class that can be remotely manipulated by passing values. Datasets and DataTable also have the same capabilities, especially in interoperable scenarios.

Create Dataviewpublic DataView (); Public DataView (DataTable);

DataView is only available if it is connected to a DataTable object that already exists and is probably not empty. Typically, this connection is specified at construction time. DataView DV; DV = new DataView (thedataset.tables["Employees"]);

However, you can also create a new view first, and then associate the table with the Tables property. DataView dv = new DataView (); Dv. Table = thedataset.tables["Employees"]; The DataView constructor allows you to get a DataView object from the DataTable. If necessary, vice versa. In fact, the DefaultView property of the DataTable object returns a DataView object for that table. DataView dv = dt. DefaultView;

Once you have the DataView object, you can use its properties to build the set of data you want the user to see. Generally, you can use the following properties:

RowFilter Sort The former can customize the rules that the visible data in the view should match. The latter is sorted by an expression. Of course you can use any combination of the two.

Set Filter RowFilter is a read-write property that reads and sets the expression for table filtering. Public virtual string RowFilter {get; set;}

You can use column names, logical and numeric operators, and any legitimate combination of constants to form an expression. Here are some examples: DV. RowFilter = "Country = ' USA '"; Dv. RowFilter = "EmployeeID >5 and Birthdate < #1/31/82#" DV. RowFilter = "Description like ' *product* '"

Let's look at the basic rules and operators for filters. A filter string is a logical connection to an expression. You can use And,or,not to connect to a shorter expression, or you can use parentheses to form clauses that specify the first operation. Clauses that typically contain column names are compared with letters, numbers, dates, or another column name. Here, you can use relational operators and arithmetic operators, such as >=, <,,, +, *,% (modulo), and so on. If the row you want to select is not easily expressed by arithmetic or logical operators, you can use the in operator. The following code shows how to pick a random line: DV. RowFilter = "EmployeeID in (2,4,5)"

You can also use wildcard characters * and%, which are more useful when used with the LIKE operator. They all represent any number of characters that can be used in lieu of each other. Note that if you already have a * or% character in the LIKE clause, you must enclose it in square brackets to avoid ambiguity. If, unfortunately, the parentheses of the string itself exist, it must also be enclosed in itself. In this way, the matching statement looks like this: DV. RowFilter = "Description like" [[[]*[]]product[[]*[]] "

Wildcards are only allowed at the beginning or end of a filtered string, and cannot appear in the middle of a string. For example, the following statement produces a run-time error: DV. RowFilter = "Description like ' prod*ct"

The string must be enclosed in single quotation marks, and the date type must be enclosed in a # symbol. Character 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 the introduction to RowFilter expressions, let's discuss three convenient functions: LEN,IIF and substring. As its name, Len () returns the length of a particular expression. The expression can be a column name, or it can be another valid expression. Substring () returns a character string of a specific length, starting at a specific position, from a specified expression. My favorite is IIf (), which has one to two values according to the value of the logical expression. IIF is a compact expression of the If-then-else statement. The syntax is as follows: IIF (expression, if_true, If_false)

With this function, a very complex filter string can be created. For example, suppose you get the Employees table from the Northwind database of SQL Server, the following expression can select those EmployeeID less than 6 and LastName to even number of characters and EmployeeID is greater than 6 and LastName is an odd number of characters in an employee. IIF (Employeeid<6, Len (lastname)%2 =0, Len (lastname)%2 >0)

Shows the results (the sample application will be discussed later)

Figure three filtering the tables in Northwind The program is an Windows®form application that uses two DataGrid controls to implement the Master/detail structure. A grid is generated when it is loaded, that is, after SQL Server data adapter completes the data read work. Note that the data adapter is introduced in Beta 2, and the Sqldatasetcommand class is the corresponding one in Beta 1.

Pre-layout view in the example above, the DataGrid must be responsible for data rows in the pre-layout view in order to refresh the user interface. This automatic mechanism is the product of. NET data binding. The DataGrid is a data-bound control that obtains data through the DataSource property. DataView is a data-bound class that can build the contents of a DataSource property.

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

DataTable, DataView, Dataset differences

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: 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.