C # DataSet: DataSet object

Source: Internet
Author: User

C # DataSet: DataSet object

ADO. A prominent feature of the NET data access technology is that it supports offline access. The core of this offline access technology is the DataSet object, which enables offline access by storing data in the memory.

DataSet object Overview

DataSet objects are composed of a group of able objects that are associated with DataRelation objects. These DataSet objects include the Rows set and Columns set. The Rows set consists of multiple DataRow objects, and the Columns set consists of multiple DataColumn objects.

Because DataSet objects are similar to databases, you can access DataSet as you access relational databases. For example, you can add or delete tables in DataSet, query data in tables, and delete data.

DataSet objects are commonly used in the following ways:

 

Merge DataSet content

The preceding table shows that the content in the DataSet object is merged using the Merge method. The Merge method has multiple reloads, but we usually encounter three common reloads. Therefore, we only illustrate some common reloads:

(1) DataSet object. Merge (DataRow []); // merges the array of DataRow objects into the current DataSet.

(2) DataSet object. Merge (DataTable); // merges the specified able and Its schema into the current DataSet.

(3) DataSet object. Merge (DataSet); // merges DataSet and Its schema into the current DataSet.

Example: Create a DataSet using the second overload method. The DataSet contains a table, two columns, and six rows of data. Create the second able that is the same as the first table. Add two rows to the second table, and then merge the table into DataSet. The complete code is:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data; // namespace ConsoleApplication4 {class Program {public static void PrintValues (DataSet ds. tables) {Console. writeLine ("table name:" + table. tableName); foreach (DataRow row in table. rows) {foreach (DataColumn column in table. columns) {Console. write (row [column] + "");} Console. writeLine () ;}} static void Main (string [] args) {DataSet ds = new DataSet ("dsText "); // create a DataSet object DataTable dt = new DataTable ("tableText"); // create a ds able object ds. tables. add (dt); // Add the DataTable object to ds DataColumn dc1 = new DataColumn ("id", Type. getType ("System. int32 ")," "); // create the first column DataColumn dc2 = new DataColumn (" Item ", Type. getType ("System. string ")," "); // create the second column dt. columns. add (dc1); // Add a column of dt to the able. columns. add (dc2); // Add a column for (int I = 0; I <6; I ++) to the able) // Add data to dt {DataRow dr = dt. newRow (); // create a new row in dt, dr ["id"] = I; // insert data into the first column, dr ["Item"] = "Item" + I; // insert data into the second column dt. rows. add (dr); // Add a row to dt} ds. acceptChanges (); // load the data Console after the last change. writeLine ("dataset before merging"); Program. printValues (ds); // outputs the data DataTable dt1 = dt. clone (); // Clone dt DataRow newRow; newRow = dt1.NewRow (); // Add row newRow ["id"] = 0; newRow ["Item"] = ""; dt1.Rows. add (new object [] {8, "item 8"}); dt1.Rows. add (new object [] {9, "item 9"}); ds. merge (dt1); // Merge dt1 into the ds Console. writeLine ("\ n" + "merged dataset"); Program. printValues (ds); // outputs the data Console in ds. readLine ();}}}

The running result is:

 

Copy DataSet content

The above table shows that the content of the DataSet object to be copied has two forms: one is to copy the result of the DataSet but not to copy the data, which is implemented by the Clone method. The format is DataSet object. clone (); the other is to Copy both the structure and data, which is implemented by the Copy method. The format is DataSet object. copy ();

Instance: Create a datasdataset DataSet, which contains a table, two columns, and six rows of data, and then create two DataSet objects respectively, complete code for copying DataSet content by using the Clone method and Copy method respectively:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data; // namespace ConsoleApplication4 {class Program {public static void PrintValues (DataSet ds. tables) {Console. writeLine ("table name:" + table. tableName); foreach (DataRow row in table. rows) {foreach (DataColumn column in table. columns) {Console. write (row [column] + "");} Console. writeLine () ;}} static void Main (string [] args) {DataSet ds = new DataSet ("dsText "); // create a DataSet object DataTable dt = new DataTable ("tableText"); // create a ds able object ds. tables. add (dt); // Add the DataTable object to ds DataColumn dc1 = new DataColumn ("id", Type. getType ("System. int32 ")," "); // create the first column DataColumn dc2 = new DataColumn (" Item ", Type. getType ("System. string ")," "); // create the second column dt. columns. add (dc1); // Add a column of dt to the able. columns. add (dc2); // Add a column for (int I = 0; I <6; I ++) to the able) // Add data to dt {DataRow dr = dt. newRow (); // create a new row in dt, dr ["id"] = I; // insert data into the first column, dr ["Item"] = "Item" + I; // insert data into the second column dt. rows. add (dr); // Add a row to dt} ds. acceptChanges (); // load the data Console after the last change. writeLine ("Source dataset"); Program. printValues (ds); // outputs the data in the ds table DataSet ds1 = ds. clone (); // replication framework DataSet ds2 = ds. copy (); // Copy the framework and data Console. writeLine ("\ n" + "Clone method"); Program. printValues (ds1); // outputs the data Console in ds. writeLine ("\ n" + "Copy method"); Program. printValues (ds2); // outputs the data Console in ds. readLine ();}}}

The running result is:

 

 

 

 

Related Article

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.