Solved

VB.net adding code to find the joins between SQL tables

Posted on 2012-04-02
2
272 Views
Last Modified: 2012-04-04
Hi. I am using the following code to get the names of a SQL database. What code would I add to find relationships between any of the tables. Thanks


 Public Sub Populate_TreeView_From_SQL()

        Dim SQLExpression As String = _
            "SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.columns ORDER BY TABLE_NAME,COLUMN_NAME"

        Dim Rootnode As TreeNode = Nothing
        Dim Mainnode As TreeNode = Nothing
        Dim Childnode As TreeNode = Nothing

        Dim MainName As String = String.Empty

        Dim oConnectionString As String = Me.RichTextBox1.Text
        Dim cn As New SqlConnection(oConnectionString)

        Dim adp As New SqlDataAdapter(SQLExpression, cn)

        Dim ds As New DataSet

        adp.Fill(ds, "SystemData")

        Me.TreeView_From.Nodes.Clear()
        'Dim Con As String = "server=196.220.43.247,1444;uid=murbro73;pwd=chestnut3;database=test"
        Rootnode = Me.TreeView_From.Nodes.Add(key:="Root", text:="database name here", _
                                        imageIndex:=0, selectedImageIndex:=0)



        For Each row As DataRow In ds.Tables("SystemData").Rows

            If MainName <> row(0).ToString Then
                Mainnode = Rootnode.Nodes.Add(key:="Table", text:=row(0).ToString, _
                imageIndex:=1, selectedImageIndex:=1)
                MainName = row(0).ToString
            End If

            Childnode = Mainnode.Nodes.Add(key:="Column", text:=row(1).ToString, _
            imageIndex:=2, selectedImageIndex:=2)

        Next

        Me.TreeView_From.Nodes(0).EnsureVisible()
        Me.TreeView_From.ExpandAll()
        Me.TreeView_From.Scrollable = True

        ds.Dispose()
        ds = Nothing
        adp.Dispose()
        adp = Nothing
        cn.Dispose()
        cn = Nothing

    End Sub
0
Comment
Question by:murbro
2 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 37799462
SQL SERVER – Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
http://www.javaxt.com/Tutorials/Visual_Studio/Compiling_64_bit_apps_using_Visual_Studio_6_and_Windows_Platform_SDK
0
 

Author Closing Comment

by:murbro
ID: 37804962
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

13 Experts available now in Live!

Get 1:1 Help Now