Sie sind auf Seite 1von 3

23.

Relations in a dataset
A dataset can hold multiple tables at the same time. The DataTable objects are collected in the
DataSet object’s Tables property. Besides of holding multiple tables, DataSet object has a
property named Relations for holding information of relations between its tables. The relations
are defined in VB.NET as DataRelation objects.

23.1. Creating relations


Two tables are related to each other through their ‘key columns’. In VB.NET the relation is
constructed by creating a DataRelation object and defining its parent column and child
column properties to point to the ‘key columns’ of the tables to be related.

DataSet
DataRelation
MyCustomer
Name CustID Name
ParentColumn -- --
ChildColumn -- --
-- --
MyOrders
CustID ProdID Qty P
-- -- -- --
-- -- -- --

When a DataRelation object is constructed, it is given a string value for its Name property and
a DataColumn object of both of the DataTable objects being part of the relation. The other
DataColumn is defined as a parent column and the other one as a child column, depending on
the nature of the relationship. This is similar to a primary key / foreign key relationship.
After creating the DataRelation, it is added to the Relations collection of the corresponding
dataset.

For example:
'Define the parent and child columns
Dim parentCol, childCol As DataColumn
parentCol = MyDataSet.Tables("MyCustomer").Columns("CustID")
childCol = MyDataSet.Tables("MyOrders").Columns("CustID")
'Define the DataRelation object
Dim relCustOrder As DataRelation
relCustOrder = _
New DataRelation("CustomerOrders", parentCol, childCol, False)
'Add the relation to the DataSet.
MyDataSet.Relations.Add(relCustOrder)
23.2. Using relations
One of the primary functions of the DataRelation object is to allow navigation from one
DataTable to another within a DataSet. This allows one to retrieve all the related DataRow
objects in one DataTable when given a single DataRow from a related DataTable.

23.2.1. Fetching the ChildRows


For example, after establishing a DataRelation between a table of customers and a table of
orders (as in the example above), one can retrieve all the order rows for a particular customer
row by using the DataRow.GetChildRows method.

For example:
Dim custRow, orderRow As DataRow
For Each orderRow In custRow.GetChildRows("CustomerOrders")
TextBox1.AppendText(orderRow("ProdID") & ControlChars.NewLine)
Next

23.2.2. Fetching the ParentRow


Correspondingly as getting the child rows of a data row in a parent table, a parent row of a data
row in a child table can be fetched through the relation by using the DataRow.GetParentRow
method.

Example:
Get the name of the customer of a certain order.
Dim custRow, orderRow As DataRow
custRow = orderRow.GetParentRow("CustomerOrders")
TextBox1.Text = custRow("Name")

23.3. The effect of relationships on the DataGrid control


If the DataSource of a DataGrid control is set to a DataSet that contains DataRelation objects,
parent tables will appear with a plus sign (+) in each row header. Clicking the plus sign causes
a node to appear that contains links to child tables. When clicking the link, one can navigate to
the child table.

For example:
'In the code the binding between the dataset and the datagrid
'happens exactly the same way as before. The programmer
'doesn’t have to take care of the existent of probable relations
DataGrid1.DataSource = MyDataSet
DataGrid1.DataMember = "MyCustomer"

23.4. Example
Fetch all the data from tables customer and orders into the dataset, create a relationship
between those two tables and show the data on a Windows form by using the DataGrid control.
'Define the data objects needed
Dim MyConnection As New OleDbConnection()
Dim MyDataAdapter As New OleDbDataAdapter()
Dim MyDataSet As New DataSet()
Dim parentCol, childCol As DataColumn
Dim relCustOrder As DataRelation
'Define the target (data source) of the connection
MyConnection.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0; _
Data Source=c:\databases\CustomerOrders.mdb;"
'Define the SelectCommand of the data adapter
MyDataAdapter.SelectCommand = New OleDbCommand()
MyDataAdapter.SelectCommand.CommandText = _
"SELECT * FROM Customer"
MyDataAdapter.SelectCommand.Connection = MyConnection
'Open the connection
MyConnection.Open()
'Fill the dataset with the customers
MyDataAdapter.Fill(MyDataSet, "MyCustomer")
'Fill the dataset with the orders
MyDataAdapter.SelectCommand.CommandText = "SELECT * FROM Orders"
MyDataAdapter.Fill(MyDataSet, "MyOrders")
'Close the connection
MyConnection.Close()
'Define the parent and child columns
parentCol = MyDataSet.Tables("MyCustomer").Columns("CustID")
childCol = MyDataSet.Tables("MyOrders").Columns("CustID")
'Create the DataRelation
relCustOrder = _
New DataRelation("CustomerOrders", parentCol, childCol, False)
'Add the relation to the DataSet.
MyDataSet.Relations.Add(relCustOrder)
'Display the dataset in the DataGrid
DataGrid1.DataSource = MyDataSet
DataGrid1.DataMember = "MyCustomer"

Das könnte Ihnen auch gefallen