Beruflich Dokumente
Kultur Dokumente
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.
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.
For example:
Dim custRow, orderRow As DataRow
For Each orderRow In custRow.GetChildRows("CustomerOrders")
TextBox1.AppendText(orderRow("ProdID") & ControlChars.NewLine)
Next
Example:
Get the name of the customer of a certain order.
Dim custRow, orderRow As DataRow
custRow = orderRow.GetParentRow("CustomerOrders")
TextBox1.Text = custRow("Name")
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"