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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
ASKER
Thanks IJZ and slightwv, I'm going to try and apply both of these suggestions and see what the results are.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("MyRelationNa me", _
ds.Tables(0).Columns("Item 1"), ds.Tables(1).Columns("Item 1"), False)
C#
DataRelation Relation1 = new DataRelation("MyRelationNa me", ds.Tables(0).Columns("Item 1"), ds.Tables(1).Columns("Item 1"), 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. ;)
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("MyRelationNa
ds.Tables(0).Columns("Item
C#
DataRelation Relation1 = new DataRelation("MyRelationNa
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;
}
ASKER
Thanks for all comments and contributions.
http://www.java2s.com/Code/CSharp/Database-ADO.net/PopulateaDataSetwithmultipleDataTableobjectsusingmultipleSELECTstatements.htm
http://www.easysoft.com/developer/languages/csharp/ado-net-odbc.html
http://www.scribd.com/doc/53138145/An-Introduction-to-ADO-Net