Avatar of Ibs
Ibs
 asked on

Typed datasets: Filling related datatables in C#

Hi,

I'm trying to fill a typed dataset that has 4 related tables i.e. 1 main parent table and 3 child tables.
I have my dataset structure setup in my C# Winforms application in VS2010.

How do I get data from my database into my dataset which contains related tables all in one SQL statement?

I have successfully used the dataAdapter.Fill method on the parent table, but not sure how to get the child tables filled with related data at the same time.

I'm also trying to avoid having SQL strings at form level , as we are going to push all the SQL and data access functionality into one assembly.

Many thanks,

Ibs.




C#.NET ProgrammingOracle Database

Avatar of undefined
Last Comment
Ibs

8/22/2022 - Mon
SOLUTION
Imran Javed Zia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Imran Javed Zia

slightwv (䄆 Netminder)

can you UNION all the selects into a single statement?

select col1,col2 from tab1
union all
select col1,col2 from tab2
union all
select col1,col2 from tab3
union all
select col1,col2 from tab4


If not, can you provide a little more detail about the requirements?
Ibs

ASKER

Thanks IJZ and slightwv, I'm going to try and apply both of these suggestions and see what the results are.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
rgn2121

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rgn2121

I don't know of any way that you can run one sql statement, but have that data end up in different datatables on the Fill().  If this is possible, I would like to see it done.  
ASKER CERTIFIED SOLUTION
Ibs

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rgn2121

I don't use my relationships for binding, but I would assume you can...

As for the way to access the values of the current record, I assume you want to know how to access the child records when you are at the parent record?

What I do is pass the DataRow that I am currently at to a function to read specific items from the child records.  So if I had
For each row as DataRow in MyParentTable.Rows

Next

I would pass the row object to a method to look at the associated child records.  The code for that method would look something like what I have listed below...

The MyRelationName is NOTthe name of the object as in Dim MyRelationName as DataRelation.  It IS the name used when you are supplying the parent and child column info for the relation...
VB.Net
Dim Relation1 As New DataRelation("MyRelationName", _
            ds.Tables(0).Columns("Item1"), ds.Tables(1).Columns("Item1"), False)
C#
DataRelation Relation1 = new DataRelation("MyRelationName", ds.Tables(0).Columns("Item1"), ds.Tables(1).Columns("Item1"), false);

Not much of a C# programmer so I hope the conversion worked.  Also, this is code a wrote few years ago that I should probably go back and check out now that I know a little more. ;)


--------VB.Net
Private Sub DoSomething(ByVal dr As DataRow)
Dim tempDR As DataRow

If dr.GetChildRows("MyRelationName").Length > 0 Then
  tempDR = CType(dr.GetChildRows("MyRelationName").GetValue(0), DataRow)
  'Now tempDR has all the related records from the child table
  'you might have to loop through these to get what you want depending on your data.
End If

tempDr=Nothing
End Sub

----------C#
private void DoSomething(DataRow dr)
{
	DataRow tempDR = null;

	if (dr.GetChildRows("MyRelationName").Length > 0) {
		tempDR = (DataRow)dr.GetChildRows("MyRelationName").GetValue(0);
		//Now tempDR has all the related records from the child table
		//you might have to loop through these to get what you want depending on your data.
	}

	tempDR = null;
}

Open in new window

Ibs

ASKER
Thanks for all comments and contributions.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.