Export (0) Print
Expand All

Nested DataRelations

In a relational representation of data, individual tables contain rows that are related to one another using a column or set of columns. In the ADO.NET DataSet, the relationship between tables is implemented using a DataRelation. When you create a DataRelation, the parent-child relationships of the columns are managed only through the relation. The tables and columns are separate entities. In the hierarchical representation of data that XML provides, the parent-child relationships are represented by parent elements that contain nested child elements. To facilitate the nesting of child objects when a DataSet is synchronized with an XmlDataDocument or written as XML data using WriteXml, the DataRelation exposes a Nested property. Setting the Nested property of a DataRelation to true causes the child rows of the relation to be nested within the parent column when written as XML data or synchronized with an XmlDataDocument. The Nested property of the DataRelation is false, by default. For example, consider the following DataSet:

Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;" & _
                                            "Integrated Security=SSPI;Initial Catalog=Northwind;")
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
Dim orderDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID, CustomerID, OrderDate FROM Orders", nwindConn)

nwindConn.Open()

Dim custDS As DataSet = New DataSet("CustomerOrders")
custDA.Fill(custDS, "Customers")
orderDA.Fill(custDS, "Orders")

nwindConn.Close()

Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders",
                     custDS.Tables("Customers").Columns("CustomerID"),
                     custDS.Tables("Orders").Columns("CustomerID"))
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;" +
                                            "Integrated Security=SSPI;Initial Catalog=Northwind;");
SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
SqlDataAdapter orderDA = new SqlDataAdapter("SELECT OrderID, CustomerID, OrderDate FROM Orders", nwindConn);

nwindConn.Open();

DataSet custDS = new DataSet("CustomerOrders");
custDA.Fill(custDS, "Customers");
orderDA.Fill(custDS, "Orders");

nwindConn.Close();

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

Because the Nested property of the DataRelation object is not set to true for this DataSet, the child objects will not be nested within the parent elements when this DataSet is represented as XML data.

The following code example shows the output that will result from calling WriteXml on the DataSet.

<CustomerOrders>
  <Customers>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
  </Customers>
  <Customers>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
  </Customers>
  <Orders>
    <OrderID>10643</OrderID>
    <CustomerID>ALFKI</CustomerID>
    <OrderDate>1997-08-25T00:00:00</OrderDate>
  </Orders>
  <Orders>
    <OrderID>10692</OrderID>
    <CustomerID>ALFKI</CustomerID>
    <OrderDate>1997-10-03T00:00:00</OrderDate>
  </Orders>
  <Orders>
    <OrderID>10308</OrderID>
    <CustomerID>ANATR</CustomerID>
    <OrderDate>1996-09-18T00:00:00</OrderDate>
  </Orders>
</CustomerOrders>

Note that the Customers element and the Orders elements are shown as sibling elements. If you wanted to have the Orders elements show up as children of their respective parent elements, the Nested property of the DataRelation would need to be set to true and you would add the following:

custOrderRel.Nested = True
[C#]
custOrderRel.Nested = true;

The following code shows what the resulting output would look like, with the Orders elements nested within their respective parent elements.

<CustomerOrders>
  <Customers>
    <CustomerID>ALFKI</CustomerID>
    <Orders>
      <OrderID>10643</OrderID>
      <CustomerID>ALFKI</CustomerID>
      <OrderDate>1997-08-25T00:00:00</OrderDate>
    </Orders>
    <Orders>
      <OrderID>10692</OrderID>
      <CustomerID>ALFKI</CustomerID>
      <OrderDate>1997-10-03T00:00:00</OrderDate>
    </Orders>
    <CompanyName>Alfreds Futterkiste</CompanyName>
  </Customers>
  <Customers>
    <CustomerID>ANATR</CustomerID>
    <Orders>
      <OrderID>10308</OrderID>
      <CustomerID>ANATR</CustomerID>
      <OrderDate>1996-09-18T00:00:00</OrderDate>
    </Orders>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
  </Customers>
</CustomerOrders>

See Also

XML and the DataSet | Adding a Relationship between Tables | Creating and Using DataSets

Show:
© 2014 Microsoft