Basic usage of DataSet and DataTable, datasetdatatable

Source: Internet
Author: User

Basic usage of DataSet and DataTable, datasetdatatable

Http://files.cnblogs.com/files/monkeyZhong/DataSetExample.rar

 

When designing table data such as database operations or XML operations, we will inevitably deal with DataSet and DataTable. Here we will introduce the usage of these classes:

First, we will introduce DataTable, which is actually a two-dimensional table, which is not so outstanding. Here is an example of construction:

DataTable dt = new DataTable ("MERs"); dt. columns. add ("CustomerID", typeof (Int32); dt. columns. add ("CustomerName", typeof (String); dt. columns. add ("Description", typeof (string); DataRow row = dt. newRow (); row [0] = 001; row [1] = ""; row [2] = "mid-range vehicles"; dt. rows. add (row); DataRow row2 = dt. newRow (); row2 [0] = 002; row2 [1] = "Mercedes-Benz"; row2 [2] = "luxury car"; dt. rows. add (row2 );

This constructs a two-row and three-column orders table.

Secondly, DataSet: we can regard it as a collection container of DataTable, And we can add many tables to it. For example:

1 customerOrders = new DataSet (); 2 customerOrders. tables. add (customerTable); 3 customerOrders. tables. add (orderTable); 4 mermerorders. tables. add (orderDetailTable); 5 customerOrders. tables. add (ProductTable );View Code

Four tables are added here.

Of course, DataSet has other content. Here I usually use less but it is very important to use DataRelation. We can think of creating a connection between a table and a table through a column, in this way, we can use GetChildRows of a row to get the row set of the Child column (satisfying the relationship), or get the row of the parent column through GetParentRow.

1 DataRelation customerOrdersRelation = new DataRelation ("CustOrders", 2 customerOrders. tables ["MERs"]. columns ["mermerid"], 3 customerOrders. tables ["Orders"]. columns ["mermerid"]); 4 customerOrders. relations. add (customerOrdersRelation); 5 DataRelation orderDetailRelation = customerOrders. relations. add ("OrderDetail", 6 mermerorders. tables ["Orders"]. columns ["OrderID"], 7 mermerorders. tables ["OrderDetails"]. columns ["OrderID"], false); 8 DataRelation orderProductRelation = customerOrders. relations. add ("OrderProducts", 9 mermerorders. tables ["Product"]. columns ["ProductID"], 10 mermerorders. tables ["OrderDetails"]. columns ["ProductID"]); 11 12 DataRow [] rows = customerOrders. tables ["MERs"]. rows [0]. getChildRows (customerOrdersRelation); 13 DataTable dt = orderTable. clone (); 14 foreach (DataRow row in rows) 15 {16 dt. importRow (row); 17 18} 19 20 StringBuilder sw = new StringBuilder (); 21 foreach (DataRow custRow in customerOrders. tables ["MERs"]. rows) 22 {23 sw. appendLine ("Customer ID:" + custRow ["CustomerID"]); 24 foreach (DataRow orderRow in custRow. getChildRows (customerOrdersRelation) 25 {26 sw. appendLine ("Order ID:" + orderRow ["OrderID"]); 27 sw. appendLine ("\ tOrder Date:" + orderRow ["DateTime"]); 28 foreach (DataRow detailRow in orderRow. getChildRows (orderDetailRelation) 29 {30 sw. appendLine ("\ tProduct" + detailRow. getParentRow (orderProductRelation) ["ProductName"]); 31} 32} 33}View Code

There can be many child rows, but the parent row has only one row.
As for the operations on Foreign keys, primary keys, constraints, and updating tables, we will not describe them here because it involves a lot of knowledge about the database. We will focus on the usage of these two classes.

 

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.