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
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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