Retrieving tables *and* their relations from database

A

Avery Scott

Hi all,

I am very new to ADO.NET (have to use version 1.1) and database
programming in general so please forgive me if my questions appear to be
naive. That being said, here is my problem ...

I have a database with some tables and relations between them. I try to
fill a DataSet with those tables *and* their relations by using a
SqlDataAdapter. My code looks like this:

public DataSet GetDataSet(string tablename, SqlConnection sqlConn)
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqpDataAdapter();
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.SelectCommand = new SqlCommand("select * from " + tablename, sqlConn);
adapter.Fill(ds, tablename);
return ds;
}

What I get is one table. What I really want is the table and the tables
that are related to this one by their foreign keys so that I can follow
its relations. Ideally my DataSet contains all this but I neither know how
to formulate the right SQL query nor how to utilize the ADO.NET API to
accomplish this. I would be grateful if someone could sketch a solution to
this problem--thanks in advance.

Regards,
Avery Scott.
 
G

GrantMagic

You need to create a relationship between the table you are retrieving.
To do this, you'll need to know exactly what you are retrieving before hand,
and then create the relationships.

Here is an article of an example of relating two datatables within a dataset
and then looping through them getting the data out.
http://www.c-sharpcorner.com/database/DataRelationVK.asp

A similair concept can be used within datasets to display data in datagrid,
nesting a datagrid within another
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top