Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net - Find SQL Table relationships

Posted on 2013-12-09
3
Medium Priority
?
286 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:Murray Brown
[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
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 2000 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:Murray Brown
ID: 39711081
Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

604 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