1.定義DataSet對(duì)象和數(shù)據(jù)表Customers、Orders
DataSet ds = new DataSet("CustomerDS"); DataTable tabCus = new DataTable("Customers"); DataTable tabOrders = new DataTable("Orders"); 2.定義表的列并添加到表中,最后將表添加到記錄集ds中
(1)表Customers的數(shù)據(jù)列 DataColumn dcCustomerID = new DataColumn("CustomerID", System.Type.GetType("System.Int32")); DataColumn dcCustomerName = new DataColumn("CustomerName", System.Type.GetType("System.String")); (2)表Orders的數(shù)據(jù)列 DataColumn dcOrderID = new DataColumn("OrderID", System.Type.GetType("System.Int32")); DataColumn dcOrdersCustomerID = new DataColumn("CustomerID", System.Type.GetType("System.Int32")); (3).將列添加到表中并設(shè)置主鍵 tabCus.Columns.Add(dcCustomerID); tabCus.Columns.Add(dcCustomerName); tabCus.PrimaryKey = new DataColumn[] { dcCustomerID }; tabOrders.Columns.Add(dcOrderID); tabOrders.Columns.Add(dcOrdersCustomerID); tabOrders.PrimaryKey = new DataColumn[] { dcOrderID }; ds.Tables.Add(tabCus); ds.Tables.Add(tabOrders); 3.創(chuàng)建DataRelation并添加到記錄集中
(1)創(chuàng)建關(guān)聯(lián)字段 DataColumn parent = ds.Tables["Customers"].Columns["CustomerID"]; DataColumn child = ds.Tables["Orders"].Columns["CustomerID"]; (2)創(chuàng)建DataRelation對(duì)象 DataRelation customerOrder = new DataRelation("customerOrder", parent, child); (3)在記錄集ds中添加關(guān)系 ds.Relations.Add(customerOrder); 4.為表添加記錄
(1)為表Customers添加數(shù)據(jù) DataRow row1 = tabCus.NewRow(); row1["CustomerID"] = 10000; row1["CustomerName"] = "張玲玲"; DataRow row2 = tabCus.NewRow(); row2["CustomerID"] = 10001; row2["CustomerName"] = "李娜"; tabCus.Rows.Add(row1); tabCus.Rows.Add(row2); (2)為表Orders添加數(shù)據(jù) DataRow rowOrder1 = tabOrders.NewRow(); rowOrder1["OrderID"] = 1; rowOrder1["CustomerID"] = 10000; DataRow rowOrder2 = tabOrders.NewRow(); rowOrder2["OrderID"] = 2; rowOrder2["CustomerID"] = 10000; DataRow rowOrder3 = tabOrders.NewRow(); rowOrder3["OrderID"] = 3; rowOrder3["CustomerID"] = 10001; tabOrders.Rows.Add(rowOrder1); tabOrders.Rows.Add(rowOrder2); tabOrders.Rows.Add(rowOrder3); 5.數(shù)據(jù)集接受數(shù)據(jù)的修改
ds.AcceptChanges(); 6.顯示Orders表數(shù)據(jù),其中Orders表中的CustomerID字段應(yīng)該用Customers表中CustomerId字段所對(duì)應(yīng)的CustomerName字段替換
Response.Write("---------訂單表數(shù)據(jù)---------"); Response.Write("<table><tr><td>訂單ID</td><td>客戶姓名</td></tr>"); foreach (DataRow dr in ds.Tables["Orders"].Rows) { DataRow rowParent = dr.GetParentRow("customerOrder"); Response.Write("<tr>"); Response.Write("<td>"); Response.Write(dr["OrderID"].ToString()); Response.Write("</td>"); Response.Write("<td>"); Response.Write(rowParent["CustomerName"].ToString()); Response.Write("</td>"); Response.Write("</tr>"); } Response.Write("</table>"); 注: 1.DataSet中,使用DataRelation對(duì)象和Relations集合可以在各DataTable之間建立關(guān)聯(lián)性 (1)DataSet 中各 DataTable 之間可以建立關(guān)聯(lián)性 Primary Key ? Parent Column Foreign Key ? Child Column (2)使用DataRelation 對(duì)象與 Relations 集合 DataRelation dr= new DataRelation("CustomersOrders",ds.Tables("Customers").Columns("CustomerID"),ds.Tables("Orders").Columns ("CustomerID")) ds.Relations.Add(dr); 2.GetChildRows與GetParentRows
(1).GetChildRows,可以取得關(guān)聯(lián) Child 表格中的數(shù)據(jù) (2).GetParentRows,可以取得所關(guān)聯(lián)的 Parent 表格中的數(shù)據(jù) 可以使用Visual Designer Tools來控制表格之間的關(guān)聯(lián) |
|