国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
DataSet 中的 Relations 屬性

示例來自 MSDN,將四個(gè)表關(guān)聯(lián)在一起,適應(yīng)于在操作多表時(shí),有時(shí)需要關(guān)聯(lián)表有時(shí)不需要關(guān)聯(lián)的情況下(見 CommunityServer 的 cs_shared_Threads_GetThreadSet 存儲(chǔ)過程也屬此類應(yīng)用)。

using System;
using System.Data;
using System.Data.SqlClient;

namespace TestDataRelation
{

    class Program
    {
        static void Main()
        {
            PrintOrders("server=(local); uid=sa; pwd=; database=Test_DataSet_DataRelation");
        }

        private static void PrintOrders(string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet customerOrders = new DataSet();

                connection.Open();

                SqlDataAdapter reader;

                reader = new SqlDataAdapter("SELECT * FROM Customers", connection);
                reader.Fill(customerOrders, "Customers");

                reader = new SqlDataAdapter("SELECT * FROM Orders", connection);
                reader.Fill(customerOrders, "Orders");

                reader = new SqlDataAdapter("SELECT * FROM OrderDetails", connection);
                reader.Fill(customerOrders, "OrderDetails");

                reader = new SqlDataAdapter("SELECT * FROM Products", connection);
                reader.Fill(customerOrders, "Products");

                connection.Close();

                DataRelation customerOrdersRelation = customerOrders.Relations.Add("CustOrders", customerOrders.Tables["Customers"].Columns["CustomerID"], customerOrders.Tables["Orders"].Columns["CustomerID"]);
                DataRelation orderDetailRelation = customerOrders.Relations.Add("OrderDetail", customerOrders.Tables["Orders"].Columns["OrderID"], customerOrders.Tables["OrderDetails"].Columns["OrderID"], false);
                DataRelation orderProductRelation = customerOrders.Relations.Add("OrderProducts", customerOrders.Tables["Products"].Columns["ProductID"], customerOrders.Tables["OrderDetails"].Columns["ProductID"]);

                // 打印所有訂單及訂單詳情
                foreach (DataRow custRow in customerOrders.Tables["Customers"].Rows)
                {
                    Console.WriteLine("客戶 ID: " + custRow["CustomerID"] + "\t客戶姓名: " + custRow["CustomerName"]);
                    Console.WriteLine("----------------------------------------");

                    foreach (DataRow orderRow in custRow.GetChildRows(customerOrdersRelation))
                    {
                        Console.WriteLine("  訂單號(hào): " + orderRow["OrderID"]);
                        Console.WriteLine("\t訂單日期: " + orderRow["OrderDate"]);

                        foreach (DataRow detailRow in orderRow.GetChildRows(orderDetailRelation))
                        {
                            Console.WriteLine("\t Product: " + detailRow.GetParentRow(orderProductRelation)["ProductName"]);
                            Console.WriteLine("\t Quantity: " + detailRow["Quantity"]);
                        }

                        Console.WriteLine();
                    }
                }

            }
        }
    }

}

MSDN:

請(qǐng)注意,當(dāng)為 Customers 表和 Orders 表創(chuàng)建 DataRelation 時(shí),沒有為 createConstraints 標(biāo)志指定任何值(默認(rèn)為 true)。它假定 Orders 表中的所有行都具有一個(gè)存在于父 Customers 表中的 CustomerID 值。如果 CustomerID 存在于 Customers 表之外的 Orders 表中,則 ForeignKeyConstraint 將引發(fā)異常。

如果子列可能包含父列不包含的值,添加 DataRelation 時(shí)請(qǐng)將 createConstraints 標(biāo)志設(shè)置為 false。在該示例中,對(duì)于 Orders 表和 OrderDetails 表之間的 DataRelation,createConstraints 標(biāo)志將設(shè)置為 false。這樣,應(yīng)用程序就可以返回 OrderDetails 表中的所有記錄并只返回 Orders 表中記錄的子集,而不會(huì)生成運(yùn)行時(shí)異常。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
DataSet數(shù)據(jù)集表之間的關(guān)聯(lián)
C#實(shí)戰(zhàn)021:OleDb操作
ADO.NET的記憶碎片(三)
ADO.NET對(duì)象模型 - 技術(shù)改變世界!學(xué)習(xí)改變自己! - 博客園
8.4.2 使用多個(gè)表和關(guān)系
多線程的SqlBulkCopy批量導(dǎo)入、事務(wù)和SqlBulkCopy使用的數(shù)據(jù)集中自定義映射字段的注意事項(xiàng)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服