Ado. Dataset

Source: Internet
Author: User
Dataset is ADO. net. dataset can be regarded as a database in memory, and dataset is a set of independent data from the database. the so-called independence means that dataset is still available even if the data link is disconnected or the database is closed. dataset uses XML to describe data internally. Because XML is a language that is platform-independent and language-independent, it can also describe data with complex relationships, for example, the data of the parent-child relationship, so dataset can actually accommodate data with complex relationships and is not dependent on database links.
It is precisely because of dataset that programmers can shield the differences between databases during programming to obtain a consistent programming model. Dataset supports multiple tables, inter-Table relationships, and data constraints, which are basically consistent with the relational database model.
Dataset is the main component of the ADO. net structure. It is the cache of data retrieved from the data source in the memory. Dataset consists of a group of able objects. You can associate these objects with datarelation objects. You can also use uniqueconstraint and foreignkeyconstraint objects to implement data integrity in dataset. For more information about using dataset objects, see using dataset in ADO. net.
Although the datatable object contains data, datarelationcollection allows you to view the table hierarchy. These tables are included in the ableablecollection accessed through the tables attribute. When accessing datatable objects, note that they are case sensitive. For example, if a datatable is named "mydatatable" and another is named "mydatatable", strings used to search one of the tables are considered case-sensitive. However, if "mydatatable" exists and "mydatatable" does not exist, the search string is case-insensitive. For more information about using a able object, see create a datatable.
Dataset can read and write data and architecture as XML documents. Data and architecture can be transmitted over HTTP and used by any application on any platform that supports XML. You can use the writexmlschema method to save the schema as an XML schema, and use the writexml method to save the schema and data. To read XML documents that contain both the schema and data, use the readxml method.
In a typical multi-layer implementation, the steps used to create and refresh a dataset and update the original data in sequence include:
Use dataadapter to generate and fill each able in dataset with data from the data source.
Modify data in a single able object by adding, updating, or deleting a datarow object.
Call the getchanges method to create the second dataset that only reflects the changes made to the data.
Call the update method of dataadapter and pass the second dataset as a parameter.
Call the merge method to merge the changes in the second dataset into the first one.
Call acceptchanges for dataset. Alternatively, call rejectchanges to cancel the change. Dataset is the core object in ADO. net. dataset contains a set of able objects.
Each datatable object has some sub-objects datarow and datacolumn, which indicate the rows and columns in the database table. With these objects, you can obtain all the elements in the table, row, and column.
A common operation of dataset is to use the fill () method of the dataadapter object to fill it with data.
Access tables in Dataset:
1. Access by Table Name: thisdataset. Tables ["MERs"] specifies the MERs object
2. Access by index (the index is based on 0): thisdataset. Tables [0] specifies the first able in the dataset.
Rows and columns accessing datatable:
1. Each able has a rows attribute, which is a collection of datarow objects.
Mydataset. Tables ["MERs"]. Rows [N]
In the thisdataset datatable object customers, specify the row number N-1 (the index is based on 0 ).
2. The datarow object has an overloaded index character attribute, allowing access to each column by column name or column number.
Thisdataset. Tables ["MERs"]. Rows [N] ["companyName"] instance: using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;

Namespace datasetreader
{
Class Program
{
Static void main (string [] ARGs)
{
// String connstring = @ "Data Source = ZHANG-1D093B228; initial catalog = northwind; user id = sa; Password = sa ";
// String connstring = @ "Server =.; Integrated Security = true; database = northwind ";
// String connstring = @ "Server = ZHANG-1D093B228; Integrated Security = true; database = northwind ";
String connstring = @ "Server = localhost; Integrated Security = true; database = northwind ";

Sqlconnection thisconnection = new sqlconnection (connstring );
String [email protected] "select customerid, contactname from customers ";
Sqldataadapter thisadapter = new sqldataadapter (strsql, thisconnection );
Dataset thisdataset = new dataset ();
Thisadapter. Fill (thisdataset, "MERs ");
Foreach (datarow therow in thisdataset. Tables ["MERs"]. Rows)
{
Console. writeline (therow ["customerid"] + "\ t" + therow ["contactname"]);
}
Thisconnection. Close ();
Console. writeline ("program finished, press enter/return to continue :");
Console. Readline ();
}
}
}

In a typical multi-layer implementation, the steps used to create and refresh a dataset and update the original data in sequence include:

1. Use dataadapter to generate and fill each able in dataset with data from the data source.

2. Add, update, or delete a datarow object to change data in a single able object.

3. Call the getchanges method to create the second dataset that only reflects the changes made to the data.

4. Call the update method of dataadapter and pass the second dataset as a parameter.

5. Call the merge method to merge the changes in the second dataset into the first one.

6. Call acceptchanges for dataset. Alternatively, call rejectchanges to cancel the change.

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.