SQL multiple tables

Jason222

New member
Joined
Oct 12, 2009
Messages
1
Programming Experience
3-5
Hello,

I have a several SQL tables and I am trying to write a routine to export the records to a text file. I'm a little confused on how to set this up. For example, if I have:

Table One with fields (id,Field1,Field2)
Table Two with fields (id,FieldA,FieldB)
Table Three with fields (id,FieldZ,FieldZZ)

The id's for tables Two and Three are foreign keys based on the id of table One.

How can I read through all the records easily and write a line of text for example that is:
id,Field1,Field2,FieldA,FieldB,FieldZ,FieldZZ
repeat...
 
One suggestion is to write a query with two inner joins to return just the fields from each of the tables you want.
 
You want to read data from a database and save it to a file, so this thread really has nothing to do with WinForms. Thread moved.

As Tom suggests, you need to execute a query that joins all three tables:
VB.NET:
SELECT Table1.id, Field1, Field2, FieldA, FieldB, FieldZ, FieldZZ
FROM Table1
    INNER JOIN Table2
        ON Table1.id = Table2.id
    INNER JOIN Table3
        ON Table1.id = Table3.id
The result set of that query will be just like any other result set. You can then use a DataReader to loop through the results and write them to a file using a StreamWriter.
 
Back
Top