Share via


DataRelations İçinde Gezinme

öğesinin birincil işlevlerinden DataRelation biri, içinde DataSetbir birinden diğerine DataTable gezintiye izin vermektir. Bu, ilgili DataRow bir DataTable'dan tek bir DataRow verildiğinde tek bir DataTable'daki tüm ilgili nesneleri almanıza olanak tanır. Örneğin, bir müşteri tablosuyla sipariş tablosu arasında DataRelation oluşturduktan sonra, GetChildRows kullanarak belirli bir müşteri satırının tüm sipariş satırlarını alabilirsiniz.

Aşağıdaki kod örneği, Müşteriler tablosuyla DataSet'in Orders tablosu arasında bir DataRelation oluşturur ve her müşteri için tüm siparişleri döndürür.

DataRelation customerOrdersRelation =
    customerOrders.Relations.Add("CustOrders",
    customerOrders.Tables["Customers"].Columns["CustomerID"],
    customerOrders.Tables["Orders"].Columns["CustomerID"]);

foreach (DataRow custRow in customerOrders.Tables["Customers"].Rows)
{
    Console.WriteLine(custRow["CustomerID"].ToString());

    foreach (DataRow orderRow in custRow.GetChildRows(customerOrdersRelation))
    {
        Console.WriteLine(orderRow["OrderID"].ToString());
    }
}
Dim customerOrdersRelation As DataRelation = _
   customerOrders.Relations.Add("CustOrders", _
   customerOrders.Tables("Customers").Columns("CustomerID"), _
   customerOrders.Tables("Orders").Columns("CustomerID"))

Dim custRow, orderRow As DataRow

For Each custRow In customerOrders.Tables("Customers").Rows
    Console.WriteLine("Customer ID:" & custRow("CustomerID").ToString())

    For Each orderRow In custRow.GetChildRows(customerOrdersRelation)
        Console.WriteLine(orderRow("OrderID").ToString())
    Next
Next

Sonraki örnek, önceki örnekte dört tabloyu birbirine bağlayarak ve bu ilişkilerde gezinerek derlemektedir. Önceki örnekte olduğu gibi CustomerID, Müşteriler tablosunu Siparişler tablosuyla ilişkilendirmektedir. Müşteriler tablosundaki her müşteri için, belirli bir müşterinin sipariş sayısını ve SiparişKimliği değerlerini döndürmek için Siparişler tablosundaki tüm alt satırlar belirlenir.

Genişletilmiş örnek, OrderDetails ve Products tablolarındaki değerleri de döndürür. Siparişler tablosu, her müşteri siparişi için hangi ürünlerin ve miktarların sipariş verildiğini belirlemek için OrderID kullanan OrderDetails tablosuyla ilişkilidir. OrderDetails tablosu yalnızca sipariş edilen bir ürünün ProductID değerini içerdiğinden, OrderDetails, ProductName değerini döndürmek için ProductID kullanan Ürünlerleilişkilidir. Bu ilişkide Ürünler tablosu üst tablo, Sipariş Ayrıntıları tablosu ise alt tablodur. Sonuç olarak, OrderDetails tablosunda yinelenirken, ilgili ProductName değerini almak için GetParentRow çağrılır.

Müşteriler ve Siparişler tabloları için DataRelation oluşturulduğunda createConstraints bayrağı için hiçbir değer belirtilmediğinden (varsayılan değer true'dir) dikkat edin. Bu, Siparişler tablosundaki tüm satırların üst Müşteriler tablosunda bulunan bir CustomerID değerine sahip olduğunu varsayar. Siparişler tablosunda Müşteriler tablosunda bulunmayan bir CustomerID varsa, bir ForeignKeyConstraint özel durum oluşur.

Alt sütunda üst sütunun içermediği değerler bulunabileceğinden, DataRelation'ı eklerken createConstraints bayrağını false olarak ayarlayın. Örnekte, Orders tablosu ve OrderDetails tablosu arasındaki DataRelation için createConstraints bayrağı false olarak ayarlanmıştır. Bu, uygulamanın Bir çalışma zamanı özel durumu oluşturmadan OrderDetails tablosundaki tüm kayıtları ve Orders tablosundaki kayıtların yalnızca bir alt kümesini döndürmesini sağlar. Genişletilmiş örnek aşağıdaki biçimde çıkış oluşturur.

Customer ID: NORTS  
  Order ID: 10517  
        Order Date: 4/24/1997 12:00:00 AM  
           Product: Filo Mix  
          Quantity: 6  
           Product: Raclette Courdavault  
          Quantity: 4  
           Product: Outback Lager  
          Quantity: 6  
  Order ID: 11057  
        Order Date: 4/29/1998 12:00:00 AM  
           Product: Outback Lager  
          Quantity: 3  

Aşağıdaki kod örneği, Orders tablosundaki kayıtların yalnızca bir alt kümesi döndürülerek OrderDetails ve Products tablolarındaki değerlerin döndürüldüğü genişletilmiş bir örnektir.

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("Customer ID: " + custRow["CustomerID"]);

    foreach (DataRow orderRow in custRow.GetChildRows(customerOrdersRelation))
    {
        Console.WriteLine("  Order ID: " + orderRow["OrderID"]);
        Console.WriteLine("\tOrder Date: " + orderRow["OrderDate"]);

        foreach (DataRow detailRow in orderRow.GetChildRows(orderDetailRelation))
        {
            Console.WriteLine("\t Product: " +
                detailRow.GetParentRow(orderProductRelation)["ProductName"]);
            Console.WriteLine("\t Quantity: " + detailRow["Quantity"]);
        }
    }
}
Dim customerOrdersRelation As DataRelation = _
   customerOrders.Relations.Add("CustOrders", _
   customerOrders.Tables("Customers").Columns("CustomerID"), _
   customerOrders.Tables("Orders").Columns("CustomerID"))

Dim orderDetailRelation As DataRelation = _
   customerOrders.Relations.Add("OrderDetail", _
   customerOrders.Tables("Orders").Columns("OrderID"), _
   customerOrders.Tables("OrderDetails").Columns("OrderID"), False)

Dim orderProductRelation As DataRelation = _
   customerOrders.Relations.Add("OrderProducts", _
   customerOrders.Tables("Products").Columns("ProductID"), _
   customerOrders.Tables("OrderDetails").Columns("ProductID"))

Dim custRow, orderRow, detailRow As DataRow

For Each custRow In customerOrders.Tables("Customers").Rows
    Console.WriteLine("Customer ID:" & custRow("CustomerID").ToString())

    For Each orderRow In custRow.GetChildRows(customerOrdersRelation)
        Console.WriteLine("  Order ID: " & orderRow("OrderID").ToString())
        Console.WriteLine(vbTab & "Order Date: " & _
          orderRow("OrderDate").ToString())

        For Each detailRow In orderRow.GetChildRows(orderDetailRelation)
            Console.WriteLine(vbTab & "   Product: " & _
              detailRow.GetParentRow(orderProductRelation) _
              ("ProductName").ToString())
            Console.WriteLine(vbTab & "  Quantity: " & _
              detailRow("Quantity").ToString())
        Next
    Next
Next

Ayrıca bkz.