Solved

VB.net - Find SQL Table relationships

Posted on 2013-12-09
3
270 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

746 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

9 Experts available now in Live!

Get 1:1 Help Now