C # Data set: DataSet object

Source: Internet
Author: User

Ado. NET data access technology is a prominent feature of the support of offline access, and the implementation of this offline access technology is the core of the DataSet object, the object by the data residing in memory for offline access.

DataSet Object Overview

The DataSet object consists of a set of DataTable objects that are associated with the DataRelation object. These dataset objects also contain the Rows collection, the Columns collection, the Rows collection consists of multiple DataRow objects, and the Columns collection consists of multiple DataColumn objects.

Because the DataSet object is much like a database, you can access the dataset like a relational database, such as adding in a dataset, deleting a table, querying data in a table, deleting data, and so on.

the DataSet object has a common method:

Merging DataSet contents

As you can see in the table above, merging the contents of a DataSet object is accomplished through the merge method. The merge method has a number of overloads, but in general we encounter three common overloads, so we just show some common overloads:

(1) DataSet object. Merge (datarow[]);//merge the DataRow object array into the current dataset

(2) DataSet object. Merge (DataTable);//merge the specified DataTable and its schema into the current dataset

(3) DataSet object. Merge (DataSet);//merge the DataSet and its schema into the current dataset

Example: Use the second overloaded method to create a dataset dataset that contains one table, two columns, and six rows of data. Then create a second DataTable that is the same as the first table. Add two rows to the second table, and then merge the table into a dataset with the complete code:

<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; The namespaces introduced by using system.data;//namespace consoleapplication4{class Program {public static void Printvalues (Data Set DS)//Output data in each table {foreach (DataTable table in 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 DataSet object DataTable dt = new DataTable ("Tabletext");//Create DataTable object ds. Tables.add (DT);//Add a DataTable object to the DS DataColumn DC1 = new DataColumn ("ID ", Type.GetType (" System.Int32 ")," ");//Create first column DataColumn DC2 = new DataColumn (" Item ", Type.GetType (" System.String ")," ");//create second column dt. Columns.Add (DC1);//Add a column of DT to the DataTable.  Columns.Add (DC2);//Add a column for (int i = 0; i < 6; i++)//To the DataTable add data {DataRow to DT Dr = dt. NewRow ();//New line in DT dr["id"] = i;//Insert data into first column dr["item"] = "item" + i;//Insert data into second column D T.rows.add (DR);//Add a line of DS to DT.            AcceptChanges ();//Load last changed data Console.WriteLine ("Pre-merge data set"); Program.printvalues (DS);//Output DS table data in datatable dt1 = dt.            Clone ();//cloning 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);//merges Dt1 into DS Console.WriteLine ("\ n" + "merged data set"); ProgrAm.        Printvalues (DS);//Output DS data Console.ReadLine (); }}}</span>

The result of the operation is:

Copy DataSet contents

As you can see in the table above, there are two forms of copying the contents of a DataSet object, one of which is to copy the result of the dataset without copying the data, which is implemented by the Clone method. It is in the form of a DataSet object. Clone (); the other is that both the copy structure and the copied data are implemented by the Copy method in the Format: DataSet object. Copy ();

Example: Creating a Moth DataSet DataSet, which contains a table, two columns, and six rows of data, and then creates two DataSet objects, respectively, using the Clone method and the Copy method for the full code of the contents of the dataset:

<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; The namespaces introduced by using system.data;//namespace consoleapplication4{class Program {public static void Printvalues (Data Set DS)//Output data in each table {foreach (DataTable table in 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 DataSet object DataTable dt = new DataTable ("Tabletext");//Create DataTable object ds. Tables.add (DT);//Add a DataTable object to the DS DataColumn DC1 = new DataColumn ("ID ", Type.GetType (" System.Int32 ")," ");//Create first column DataColumn DC2 = new DataColumn (" Item ", Type.GetType (" System.String ")," ");//create second column dt. Columns.Add (DC1);//Add a column of DT to the DataTable.  Columns.Add (DC2);//Add a column for (int i = 0; i < 6; i++)//To the DataTable add data {DataRow to DT Dr = dt. NewRow ();//New line in DT dr["id"] = i;//Insert data into first column dr["item"] = "item" + i;//Insert data into second column D T.rows.add (DR);//Add a line of DS to DT.            AcceptChanges ();//load the last changed data Console.WriteLine ("Source DataSet"); Program.printvalues (DS);//The data in the table in the output DS is DataSet Ds1 = ds. Clone ();//replication framework DataSet DS2 = ds.            Copy ();//replication Framework and Data Console.WriteLine ("\ n" + "Clone Method");            Program.printvalues (DS1);//Output DS data Console.WriteLine ("\ n" + "Copy Method");        Program.printvalues (DS2);//Output DS data Console.ReadLine (); }}}</span>

The result of the operation is:



C # Data set: DataSet object

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.