Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Table A'
Dim dtbA As New DataTable("TableA")
dtbA.Columns.Add(New DataColumn("ColA", GetType(String)))
dtbA.PrimaryKey = {dtbA.Columns("ColA")}
'Table B'
Dim dtbB As New DataTable("TableB")
dtbB.Columns.Add(New DataColumn("ColB", GetType(String)))
dtbB.Columns.Add(New DataColumn("ColD", GetType(String)))
dtbB.PrimaryKey = {dtbB.Columns("ColB")}
'Table C'
Dim dtbC As New DataTable("TableC")
dtbC.Columns.Add(New DataColumn("ColA", GetType(String)))
dtbC.Columns.Add(New DataColumn("ColB", GetType(String)))
dtbC.PrimaryKey = {dtbC.Columns("ColA"), dtbC.Columns("ColB")}
'Table D'
Dim dtbD As New DataTable("TableD")
dtbD.Columns.Add(New DataColumn("ColD", GetType(String)))
'Dataset'
Dim dst As New DataSet("MyDataset")
dst.Tables.Add(dtbA)
dst.Tables.Add(dtbB)
dst.Tables.Add(dtbC)
dst.Tables.Add(dtbD)
dst.Relations.Add(New DataRelation("AC", dtbA.Columns("ColA"), dtbC.Columns("ColA"), True))
dst.Relations.Add(New DataRelation("BC", dtbB.Columns("ColB"), dtbC.Columns("ColB"), True))
dst.Relations.Add(New DataRelation("BD", dtbB.Columns("ColD"), dtbD.Columns("ColD"), True))
'now add data'
dst.Tables("TableA").Rows.Add("AA1")
dst.Tables("TableA").Rows.Add("AA2")
dst.Tables("TableA").Rows.Add("AA3")
dst.Tables("TableB").Rows.Add("BB1", "DD1")
dst.Tables("TableB").Rows.Add("BB2", "DD2")
dst.Tables("TableB").Rows.Add("BB3", "DD3")
dst.Tables("TableC").Rows.Add("AA1", "BB1")
dst.Tables("TableC").Rows.Add("AA2", "BB2")
dst.Tables("TableC").Rows.Add("AA3", "BB3")
dst.Tables("TableD").Rows.Add("DD1")
dst.Tables("TableD").Rows.Add("DD2")
dst.Tables("TableD").Rows.Add("DD3")
'query the data'
Dim s As String = ""
For Each drwA As DataRow In dst.Tables("TableA").Rows
s &= "TableA:" & drwA("ColA").ToString & vbCrLf
Dim drwC As DataRow = drwA.GetChildRows(dst.Relations("AC"))(0)
s &= "--TableC:" & drwC("ColA").ToString() & drwC("ColB").ToString() & vbCrLf
Dim drwB As DataRow = drwC.GetParentRow(dst.Relations("BC"))
s &= "--TableB:" & drwB("ColB").ToString() & drwB("ColD").ToString() & vbCrLf
Dim drwD As DataRow = drwB.GetChildRows(dst.Relations("BD"))(0)
s &= "--TableD:" & drwD("ColD").ToString() & vbCrLf
Next
MsgBox(s)
End Sub