473,473 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C#.Net Retrieving database relationships

27 New Member
Hi guys,

Just a simple question which has frustrated me for a little while searching for the answer. How do you manage to get the relationships from an external database?

I'm making a program which i am linking up databases and will give functionality to see the relationships between tables and so forth.

I have not had much luck finding any solutions

I tried something along these lines, with a table i know has relationships of both types (parent and child) but it doesn't give me a response.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SqlConnection mySqlConnection = new SqlConnection(connectionString);
  3.  
  4. SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
  5. mySqlCommand.CommandText = "SELECT * FROM MovieDetails";
  6.  
  7. mySqlConnection.Open();
  8.  
  9. SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();
  10. DataTable myDataTable = productsSqlDataReader.GetSchemaTable();
  11. if (myDataTable.ChildRelations.Count > 0) {
  12.          MessageBox.Show(myDataTable.ChildRelations[0].RelationName);
  13. }
  14. mySqlConnection.Close();
  15.  
Nov 6 '08 #1
7 2539
iam_clint
1,208 Recognized Expert Top Contributor
You can use a dataset to create child/parent relationships.

The dataset can hold as many tables as you put in it and then you can refrence these tables such as ds.tables(index) and add relationships on certain columns.
Nov 7 '08 #2
Gun Slinger
27 New Member
Hi Clint

Thanks for the reply. But i think you may have skimmed my topic slightly

I know that datasets can create and use relationships when you set them manually. But i want to recover a relationship from a database that is separate from my program completely. Is it that the relationships are not actually saved in the database itself? I find that hard to believe.

Anyway, I look forward to your next reply.
Nov 7 '08 #3
Plater
7,872 Recognized Expert Expert
can do you get something from this information:
http://developer.mimer.com/documenta...ic_views2.html

For instance to get all the columns in a table called 'mytable' I use this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM information_schema.columns
  3. WHERE TABLE_NAME= 'mytable'
  4.  
Perhaps one of the other schema things contains the relationships you wanted
Nov 7 '08 #4
Gun Slinger
27 New Member
Ah i see!

I'll have a look around them. But by you saying perhaps seems to tell me that people don't do this very often. What is the method people do usually do for this type of problem?

I'll see if i can find the solution in there, but it may not be hey?
Nov 8 '08 #5
bbnayak
1 New Member
I have to make an application which will fetch the result from a databse/table by criteria Then it will show all the result matching the criteria. i want to make the application using Dot Net 2.0. If some one can help me how to start the project and what will be the flow .

B.B.Nayak
Nov 8 '08 #6
Gun Slinger
27 New Member
You want to make a search that searches all tables? thats pretty much it?

Kind of not the same as my topic. Post a topic if you want this question answered!
Nov 8 '08 #7
Gun Slinger
27 New Member
I know this is a double post, but i seem to have found the answer i was looking for. It doesn't seem to run slowly, and gets the information that i need.

Found it here http://www.mssqltips.com/tip.asp?tip=1106 but its probably in other places.

The code is in the 5th row. The code is:

Expand|Select|Wrap|Line Numbers
  1. "SELECT OBJECT_NAME(f.constraint_object_id) AS 'ForeignKey',"+
  2. " OBJECT_NAME(f.parent_object_id) AS 'FKTable', c1.[name]" +
  3. " AS 'FKColumnName', OBJECT_NAME(f.referenced_object_id)" +
  4. " AS 'PKTable', c2.[name] AS 'PKColumnName' FROM" + 
  5. " sys.foreign_key_columns f INNER JOIN sys.all_columns c1" +
  6. " ON f.parent_object_id = c1.[object_id] AND" + 
  7. " f.parent_column_id = c1.column_id INNER JOIN" +
  8. " sys.all_columns c2 ON f.referenced_object_id =" +
  9. " c2.[object_id] AND f.referenced_column_id =" + 
  10. "c2.column_id ORDER BY OBJECT_NAME(f.referenced_object_id)"
I have to test this out a bit more, but it seems to be fine! I'm not planning on dealing with too many tables, so it shouldn't be too problematic.
Nov 8 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
2
by: Marc R. Bertrand | last post by:
Hello, Do some of you know some pretty good books or web tutorials on building databases, if any? By that I mean, when looking at a business, the sound way of choosing the proper fields and the...
11
by: dixie | last post by:
If I wanted to be able to copy all of the tables in an existing database called Original.mde to another database called New.mde from a button click event in Original.mde, is there an easy way of...
5
by: Deano | last post by:
I'm experiencing bloat problems when I restore my backup files. Users can take a look at the reports of any previous backup without having to ensure they have a backup of their current data - this...
5
by: Mike Turco | last post by:
What is the difference between creating relationships in the front-end vs. the back-end database? I was trying to create a relationship in a database front-end and noticed that I could not check...
3
by: teedilo | last post by:
Our MS SQL (SQL Server 2000) DBA has database privileges locked down pretty tightly. We end users/developers do not have administrator privileges for most databases. That arrangement has worked...
4
by: celinesuzzarini | last post by:
Hi all, I have split my database a while ago, and now, I want to add a table with relationships to other existing tables. I open the BE, create my table, and then go to the relationships...
11
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a...
5
by: mark_aok | last post by:
Hi all, I have a situation where I have a split database. At the back end, I need to - create a new table (I will call it newTable) with the exact fields, and relationships as another table...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.