Ado. Net dataset is also a good thing for me to learn.

Source: Internet
Author: User

The dataset stores data in the disconnected cache. The structure of a dataset is similar to that of a relational database. It exposes hierarchical object models of tables, rows, and columns. In addition, it contains the constraints and relationships defined for the dataset.
Source: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/vbcon/html/vbcondatasets. asp

Note:If you want to use a group of tables and rows when disconnecting from the data source, use the dataset. For data access design, using datasets is not always the best solution. For more information, see data access policy recommendations.

You can use the following sections of the. NET Framework namespace to create and operate datasets.

The basic components of a dataset are made public to you through standard programming structures (such as attributes and collections. For example:

  • The dataset class contains the tables set of the data table and the relations set of the datarelation object.
  • The datatable class contains the rows set of table rows, the columns set of data columns, and the childrelations and parentrelations set of data relations.
  • The datarow class contains the rowstate attribute. The value of this attribute indicates whether the row has been changed and how it is changed after the data table is loaded from the database for the first time. Possible rowstate attributes includeDeleted,Modified,NewAndUnchanged.
Dataset, architecture, and XML

The ADO. Net dataset is a data view in XML format. It is a data relationship view. In Visual Studio and. NET Framework, XML is the format used to store and transmit various data. Therefore, datasets are closely related to XML. This relationship between datasets and XML allows you to benefit from the following functions of datasets:

  • The structure of a dataset (tables, columns, relationships, and constraints) can be defined in the XML architecture. The XML architecture is a standard-based W3C (World Wide Web Federation) format used to define the structure of XML data. You can use the readxmlschema and writexmlschema methods to read and write the architecture that stores structured information. If no architecture is available, a dataset can be derived from the data (via its inferxmlschema method) in an XML document structured by link method. For more information about datasets and architectures, see XML architecture introduction.
  • You can generate a dataset class that incorporates schema information to define its data structures (such as tables and columns) as class members. (See "typed and untyped datasets" below ").
  • You can use the readxml method of the dataset to read XML documents or streams into the dataset, and use the writexml method of the dataset to write the dataset in XML format. Because XML is a standard data exchange format between different applications, it means that datasets containing XML format information sent by other applications can be loaded. Similarly, a dataset can be written into an XML Stream or document to share it with other applications or simply store it as a standard format.
  • You can create an XML view (xmldatadocument object) of the dataset content, and view and operate data using the relational method (through the dataset) or XML method. These two views are automatically synchronized when they are changed.
Typed and untyped Datasets

A dataset can be typed or untyped. A typed dataset first starts from the baseDatasetClass, and then use the information in the XML schema file (. XSD file) to generate a new class. Information in the architecture (tables, columns, etc.) is generated and compiled as the First Class Object and attribute of a group for this new dataset class.

Note:For more information about the dataset architecture, see XML Architecture and Data.

Because it is typedDatasetClass slave BaseDatasetClass inheritance, so this type of class undertakesDatasetAll functions ofDatasetClass instances are used together as parameter methods.

In contrast, untyped datasets do not have a built-in architecture. Like typed datasets, untyped datasets include tables, columns, and so on, but they are only published as collections. (However, after manually creating other data elements in a table or non-typed dataset, you can use the writexmlschema method of the dataset to export the structure of the dataset to the schema .)

You can use either of the two types in the application. However, Visual Studio supports more tools for typed datasets, and allows you to program datasets more easily and easily without errors.

Comparison of data access in typed and non-typed Datasets

A typed dataset class has an object model in which the First Class Object of the table and column of the dataset is located. For example, if you are using a typed dataset, you can use the following code to reference the column:

' Visual Basic' This accesses the CustomerID column in the first row of' the Customers table.Dim s As Strings = dsCustomersOrders1.Customers(0).CustomerID// C#// This accesses the CustomerID column in the first row of// the Customers table.string s;s = dsCustomersOrders1.Customers[0].CustomerID;

In comparison, if a non-Typed Dataset is used, the equivalent code is:

' Visual BasicDim s As Strings = CType(dsCustomersOrders1.Tables("Customers").Rows(0).Item("CustomerID"), String)// C#string s = (string) dsCustomersOrders1.Tables["Customers"].Rows[0]["CustomerID"];

Typed Access is not only easier to read, but also fully supported by SMART awareness in Visual Studio code editor. In addition to ease of use, the syntax of a typed dataset also provides a type check during compilation, which greatly reduces the possibility of errors when assigning values to dataset members. Access to tables and columns in a typed dataset is faster at runtime, because the access is determined at compilation rather than at runtime.

Although typed datasets have many advantages, you need to use untyped datasets in many cases. The most obvious case is that the dataset has no architecture available. For example, this occurs when an application is interacting with the component that returns the dataset and you do not know the structure of the component. Similarly, sometimes the data used does not have a static predictable structure. In this case, it is impractical to use a typed dataset, because for each change in the data structure, you must regenerate the typed dataset class.

More often, datasets with no available architecture may need to be dynamically created. In this case, a dataset is just a convenient structure that can be used to retain information (as long as the data can be expressed in a relational way ). At the same time, you can also use the dataset function, such as the ability to serialize information transmitted to another process or write XML files.

Case sensitivity of a dataset

In a dataset, the names of the following tables and columns are case-insensitive by default. That is, a table named "MERs" in the dataset may also be "customers ". This complies with naming rules for many databases, including SQL Server, that is, the names of data elements cannot be case sensitive.

Note:Different from datasets, XML documents are case sensitive. Therefore, the names of data elements defined in the schema are case sensitive. For example, the Architecture protocol allows the schema to be included to define a table named "MERs" and another different table named "customers. This results in name conflict when the schema is used to generate a dataset category. For more information, see XML elements, attributes, and types.

However, Case sensitivity can be a factor that determines how data is interpreted in a dataset. For example, when filtering data in a dataset table, the search criteria may return different results based on whether the comparison is case sensitive. By setting the casesensitive attribute of a dataset, you can control whether the filtering, search, and sorting are case sensitive. By default, all tables in the dataset inherit the value of this attribute. (This attribute can be rewritten for each separate table .)

Fill a dataset

A dataset is a container and therefore needs to be filled with data. When filling data sets, various events, application constraints check, and so on will be triggered. For more information about dataset updates and updates, see dataset updates in Visual Studio. NET.

You can fill a dataset in multiple ways:

  • TheFillMethod. This causes the adapter to execute SQL statements or stored procedures and then fill the results in the tables in the dataset. If a dataset contains multiple tables, each table may have a separate data adapter. Therefore, you must callFillMethod.

    For more information about how to fill a dataset, see data adapter introduction and create a data adapter. For more information about how to use the data adapter to populate a dataset, see populate a dataset with dataadapter.

  • By creatingDatarowObject and add them to the tableRowsSet to manually fill the tables in the dataset. (This operation can only be performed at runtime and cannot be set at design time.RowsSet .) For more information, see add data to a table.
  • Reads an XML document or stream into a dataset. For more information, see the readxml method.
  • Merge (copy) the content of another dataset. This solution is useful if the application obtains datasets from different sources (for example, different XML Web Services) but needs to merge them into a dataset. For more information, see dataset. merge.
Record location and navigation in the dataset

Because a dataset is a completely disconnected data container, a dataset (different from an ADO record set) does not need or support the concept of the current record. Instead, all records in the dataset are available.

Because there is no current record, there is no specific attribute pointing to the current record, and there is no method or attribute moving from one record to another record. (In comparison, the ADO record set supports the absolute record location and the method of moving from one record to another .) You can access tables in the form of objects in a dataset. Each table exposes a row set. You can process a row set like any set, access a row through the index of the set, or use a programming language to access a row through a specific statement of the set.

Note:If you bind controls in Windows Forms to datasets, you can use the form binding structure to simplify access to individual records. For more information, see Locating data in Windows Forms.

Related tables and datarelation objects

If the dataset contains multiple tables, the information in these tables may be related. Datasets do not have the inheritance knowledge of these relationships. Therefore, to use the data in the relevant tables, you can create a datarelation object to describe the relationships between tables in the dataset. AvailableDatarelationThe object obtains the relevant sub-records of the parent record and the parent record from the sub-records programmatically.

For example, imagine the customer and order data, such as the situation in the northwind database. The "MERs" table may contain the following records:

CustomerID   CompanyName               CityALFKI        Alfreds Futterkiste       BerlinANTON        Antonio Moreno Taquerias  Mexico D.F.AROUT        Around the Horn           London

A dataset may also contain another table containing order information. The "orders" table contains the customer ID in the form of a foreign key column. Select only some columns in the "orders" table, which may be similar to the following:

OrderId    CustomerID    OrderDate10692      ALFKI         10/03/199710702      ALFKI         10/13/199710365      ANTON         11/27/199610507      ANTON         4/15/1997

Because each customer can have more than one order, there is a one-to-many relationship between the customer and the order. For example, in the preceding table, the customer alfki has two orders.

AvailableDatarelationThe object obtains related records from the child or parent table. For example, when using a record describing the customer's Anton, you can obtain a set of records describing the customer's order. Similarly, if you use a record that describes Order Number 10507, you can useDatarelationThe object obtains the record of the customer (Anton) that describes this order.

Constraints

Like most databases, datasets also support constraints as a way to ensure data integrity. A constraint is a rule applied to insert, update, or delete rows in a table. Two types of constraints can be defined:

  • Unique constraint. Check whether the new values in the column are unique in the table.
  • Foreign key constraints define the rules for updating or deleting related sub-records when the records in the master table are updated.

In a dataset, constraints are associated with a single table (foreign key constraint) or column (unique constraint, that is, the constraint that ensures the uniqueness of column values. Constraint as typeUniqueconstraintOrForeignkeyconstraintObject implementation. Then they are added to the table's constraints set. In addition, you can set the unique attribute of the data columnTrueTo specify the unique constraint.

The dataset itself supports the Boolean enforceconstraints attribute, which specifies whether to force the constraint. By default, this attribute is setTrue. However, it is useful to temporarily disable constraints. The most common case is when the method used during the change record process temporarily leads to invalid status. You can re-enable the constraints after the changes are completed (and the valid status is returned.

In Visual Studio, constraints are implicitly created when a dataset is defined. By adding a primary key to a dataset, a unique constraint is implicitly created for the primary key column. Set the unique attribute of other columnsTrueYou can specify a unique constraint for them.

By creating a data setDatarelationObject To create a foreign key constraint. In addition to allowing you to obtain information about related records programmatically,DatarelationObjects can also define foreign key constraint rules.

ForDatarelationFor more information about using an object as a foreign key constraint, see datarelation object. For more information about creating constraints programmatically, see add constraints to tables.

Update a dataset and a data storage area

When the records in the dataset are changed, these changes must be written back to the database. To write changes from a dataset to a database, you must callUpdateTo communicate with the corresponding data source.

Used to operate on individual recordsDatarowClass inclusionRowstateAttribute. The value of this attribute indicates whether the row has been changed and how it is changed after the data table is loaded from the database for the first time. Possible values includeDeleted,Modified,NewAndUnchanged.UpdateMethod checkRowstateAttribute Value to determine which records need to be written to the database and which specific database commands should be called (add, edit, and delete ).

For more information about data update, see dataset update in Visual Studio. NET.

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.