C 2005 資料庫訪問(四)

2021-04-17 12:13:54 字數 3182 閱讀 3633

在dataset中訪問多個表。

ado.net模型與原來的資料訪問模型相比,有乙個最大的優點:dataset物件可以記錄多個表和他們之間的關係。也就是說在乙個操作的不同程式段之間傳遞完整的相關資料集,體系結構內在地維護資料之間關係的完整性。

datarelation物件用於描述在dataset中的多個datatabels之間的關係。每乙個dataset都有datarelations的datarelation集合,這可以確認和操作相關的表。

下面我們就以customers和orders表為例。在兩個表中的匹配的customerid域定義customers表和orders表之間的一對多的關係,為此要建立乙個代表這種關係的datarelation物件。

下面為導航關係**,關鍵**已經加粗:

using system;

using system.data;

using system.datasqlclient;

class datarelationexample

}

thisconnection.close();

下面將進一步擴充套件此程式,研究每一位顧客在訂單中所定購的集體產品,以及具體產品的名稱。為此需要customers,orders,orders details,products這4個表。與前面的**相似,首先初始化連線,然後為每一表建立乙個dataadapter物件:

sqldataadapter custadapter = new sqldataadapter("select * from customers",thisconnection);

custadapter.fill(thisdataset,"customers");

sqldataadapter orderadapter = new sqldataadapter("select * from orders",thisconnection);

orderadapter.fill(thisdataset,"orders");       

sqldataadapter detailadapter = new sqldataadapter("select * from [order details]",thisconnection);

detailadapter.fill(thisdataset,"order details");       

sqldataadapter prodadapter = new sqldataadapter("select * from products",thisconnection);

prodadapter.fill(thisdataset,"products");

為4個表之間的每一種關係構建datarelation物件:

datarelation custorderrel = thisdataset.relations.add("custorders",thisdata.tables["customers"].columns["customerid"],thisdata.tables["orders"].columns["customerid"]);

datarelation orderdetailrel = thisdataset.relations.add("orderdetail",thisdata.tables["orders"].columns["orderid"],thisdata.tables["order details"].columns["orderid"]);

datarelation orderprodrel = thisdataset.relations.add("orderproducts",thisdata.tables["products"].columns["productid"],thisdata.tables["order details"].columns["productid"]);

下面是這些關係的處理工作,利用乙個foreach**巢狀來完成:

foreach (datarow custrow in thisdataset.tables["customers"].rows){

console.writeline("customer id:" +custrow["customerid"]);

foreach (datarow orderrow in custrow.getchildrows(custorderrel)){

console.writeline("/torderid:" + orderrow["orderid"]);

console.writeline("/torder date:" + orderrow["orderdate"]);

foreach (datarow detailrow in orderrow.getchildrows(orderdetailrel)){

console.writeline("/t/tproduct:" + detailrow.getparentrow(orderproductrel)["productname"]);

console.writeline("/tquantity:" + detailrow["quantity"]);

上面代需要注意的地方:為了獲取product行,可以呼叫getparentrow(),可以獲得父物件,從關係的「多」端進入「一」端。有時這種從子物件進入父物件的導航也稱為「逆流」導航。從父物件進入子物件的導航也稱為「順流」導航。

childrelations和parentrelations集合

datatable具有屬性childrelatons,它儲存所有的datarelations的集合,當前的表以父關係的身份加入到datarelations中。使用方法:

foreach(datarelation thisrelation in thistable.childrelations){

foreach(datarow thisparentrow in thistable.rows){

foreach(datarow thischildrow in thisparentrow.getchildrows(thisrelation)){

//...process child rows...

在datatable上還有parentrelation集合,可以按類似的方式使用datatable。

《c#入門經典》清華大學出版社

C 2005 訪問資料庫(一)

microsoft.net framework資料庫訪問採用ado.net技術。ado.net提供兩種內建的.net資料提供者。一種用於ole db資料來源,一種用於microsoft sql server。通過ole db訪問資料格式 microsoft access 第三方資料庫 非關係資料。還...

C 2005 資料庫訪問(五)

ado.net datasets中的xml支援 在ado.net中的xml支援主要用於dataset物件,因為xml主要關注所有的關係和分層的結構化資料。dataset具有7個方法可以用於處理xml,其中最易於使用的方法是writexml 它將資料集的內容以xml文件的形式寫出。writexml 可...

C 2005 資料庫訪問(六)

從這篇開始接下來的兩篇將介紹ado.net中的sql支援。在這裡我們就不再具體介紹sql命令,這個大家可以去查相關的資料。sql使用4種命令查詢 更新 新增 刪除。我們可以用命令構建器生成sql命令,用於以select命令為基礎修改資料 update insert delete 在下面建立的程式中,...