Solved

Finding out if a column in a DataTable has a relationship to another DataTable, and if so, to which column in that DataTable

Posted on 2007-11-30
2
301 Views
Last Modified: 2010-08-05
When looping through the columns of a DataTable in a DataSet in VB 2005, how can I find out if a column is linked (i.e. has a relatioship) to another DataTable in that DataSet, and if it is linked, to which column and DataTable?

Thanks
0
Comment
Question by:SETP
2 Comments
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20382911
It's very easy, you can just loop through the Relations collection of the DataSet.  

Here's an example:

        'Create 2 tables, add them to a DataSet and create a relationship between both:
        Dim tableA As New DataTable()
        tableA.Columns.Add("tableA_ID")
        tableA.Columns("tableA_ID").DataType = GetType(Int16)
        tableA.Columns.Add("Item")

        Dim tableB As New DataTable()
        tableB.Columns.Add("tableB_ID")
        tableB.Columns("tableB_ID").DataType = GetType(Int16)
        tableB.Columns.Add("tableA_ID")
        tableB.Columns("tableA_ID").DataType = GetType(Int16)
        tableB.Columns.Add("Item")


        'Create a DataSet and add both tables
        Dim ds As New DataSet()
        ds.Tables.Add(tableA)
        ds.Tables.Add(tableB)


        'Create a relationship between both tables:
        ds.Relations.Add("tableA_tableB", _
            New DataColumn() {tableA.Columns("tableA_ID")}, _
            New DataColumn() {tableB.Columns("tableA_ID")}, _
           False)



        'Now to find out what types of relationships exist in the DataSet,
        '    between which tables, and what columns are referenced:
        For Each rel As DataRelation In ds.Relations
            Debug.WriteLine("Parent table:  " & rel.ParentTable.TableName)

            For Each col As DataColumn In rel.ParentColumns
                Debug.WriteLine(vbTab & "Parent column:  " & col.ColumnName)
            Next

            Debug.WriteLine("Child table:  " & rel.ChildTable.TableName)

            For Each col As DataColumn In rel.ChildColumns
                Debug.WriteLine(vbTab & "Child column:  " & col.ColumnName)
            Next
        Next

        Stop

0
 

Author Closing Comment

by:SETP
ID: 31411925
Thanks VBRocks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question