How select from 2 ADO.NET DataTables?

R

Ronald S. Cook

Let's say I have 2 ADO.NET DataTables (tblAuthor and tblBook) within an
ADO.NET DataSet (dstPublish).



tblAuthor has DataColumns AuthorID and AuthorName, tblBook has DataColumns
BookID, BookTitle, AuthorID (assume a book can be written by only one
author).



tblAuthor

---------

1 Ayn

2 Jim

3 Sue



tblBook

-------

10 Fountainhead 1

11 Atlas Shrugged 1

12 Moby Dick 2



I want to create a list of all books with their associated author name.



FountainHead Ayn

Atlas Shrugged Ayn

Moby Dick



This is a breeze in SQL for me, but I'm not sure how to go about using
ADO.NET DataTables. Do I need a DataRelation, or is it just a matter of
having a for loop within a for loop, or is it something else?



Thanks,

Ron
 
T

Truong Hong Thi

Do I need a DataRelation, or is it just a matter of
having a for loop within a for loop
You can create a DataRelation. The DataRow has methods for navigation
along the data relation like GetParentRows, GetParentRow, GetChildRows,
etc. Something like this:

dataSet.Relations.Add(tblAuthor.Columns["ID"],
tblBooks.Columns["AuthorID"]);
Change the column names appropriately.

foreach (book row in tblBookx)
{
Row authorRow = row.GetParentRow();
}

You still have to loop, but it simplifies parent looking-up by removing
the inner loop.

Thi
 

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