Solved

VB.net - Find SQL Table relationships

Posted on 2013-12-09
3
272 Views
Last Modified: 2013-12-11
Hi

What VB.net code would I use to find the table relationships in a SQL database?
0
Comment
Question by:murbro
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39706227
Are you planning on doing this with SQLDMO, or via a straight-forward query?
0
 
LVL 15

Accepted Solution

by:
unknown_routine earned 500 total points
ID: 39706242
You need to run the following query:

Select
	object_name(rkeyid) Parent_Table,
	object_name(fkeyid) Child_Table,
	object_name(constid) FKey_Name,
	a1.name FKey_Col,
	a2.name Ref_KeyCol
From
	sys.sysforeignkeys s
	Inner join sys.syscolumns a1
		on ( s.fkeyid = a1.id And s.fkey = a1.colid )
	Inner join syscolumns a2
		on ( s.rkeyid = a2.id And s.rkey = a2.colid )
Order by Parent_Table,Child_Table

Open in new window



So:

Step 1 : Create an SQL connection and open it

Step 2: Add the above query to a string variabe:

Dim mycommandas string ="object_name(rkeyid) Parent_Table,"
mycommandas =mycommandas +"object_name(fkeyid) Child_Table,"
........
.......
mycommandas =mycommandas +"Order by Parent_Table,Child_Table"


Step 3: Create a dataAdapter object and use it to fill a dataset with the results of the above command.
0
 

Author Closing Comment

by:murbro
ID: 39711081
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Sorting Full Text results 2 15
SQL Server Express 10 28
Sql Count with Select Distinct 4 27
SSRS Dynamic Column Names - Last 8 Weeks 3 17
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

861 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

23 Experts available now in Live!

Get 1:1 Help Now