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
299 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now