DataRelation Check?

L

localhost

I have a DataSet with 4 DataRelations inside. For each DataRelation,
I need to enumerate the tables from "bottom up". Some DataRelations
are parent-child, others are parent-child-grandchild. How can I make
a single "recursive function" to tell me which tables belong to
others?

Thanks.
 
G

Guest

You don't eally need to recurse, since all the DataRelations are contained in
the DataSet's Relarions Collection:

Dim ds As DataSet
Dim dr As DataRelation
For Each dr In ds.Relations
Dim ParentCol As DataColumn = dr.ParentColumns(0)
Dim ParentTable As DataTable = ParentCol.Table

Dim ChildCol As DataColumn = dr.ChildColumns(0)
Dim ChildTable As DataTable = ChildCol.Table

Console.WriteLine("Relation: " & dr.RelationName & " Parent: " &
ParentTable.TableName & " Child: " & ChildTable.TableName)
Next dr

But you said you wanted to go bottom-up. I don't know exactly where your
starting point should be, but given a single DataTable, this function will
recursively identify parents, grandparents, etc.

Private Function GetMyParent(ByVal dt As DataTable)
Dim dr As DataRelation
For Each dr In dt.ParentRelations
Dim ParentCol As DataColumn = dr.ParentColumns(0)
Dim ParentTable As DataTable = ParentCol.Table
Console.WriteLine("Child Table: " & dt.TableName & " Parent: "
& ParentTable.TableName)
GetMyParent(ParentTable)
Next
End Function

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top