Selecting records from multiple table

AllanJohnson

New member
Joined
May 9, 2012
Messages
1
Programming Experience
Beginner
I have a 2 table in my database. Lets call it tblStudents and tblCourse. In my tblStudents I have field names with StudentID, StudentName, StudentGender, and at my tblCourse I have StudentID, StudentCourse, StudentYear and StudentNumber. I have a windows form which is I need to display all info of a certain student. this is working perfectly
C#:
[COLOR=#000000][FONT=inherit]strSelect2 [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#008800][FONT=inherit]"select * from tblStudents where StudentID = @id"[/FONT][/COLOR][COLOR=#666600][FONT=inherit]);[/FONT][/COLOR]
but, I need to display the course and year of a certain student too. The problem is I don't know how to select multiple tables. Any help would be appreciated. Thanks in advance. God Bless
smile.gif.pagespeed.ce.Op6H3a25FY.gif
 
This question really has nothing to do with Windows Forms. Displaying the contents of a DataTable is done exactly the same way regardless of how that DataTable is populated. In fact, this question doesn't even really have anything to do with C#. Obviously you already know how to execute a query and display the results, so that's all the C# part covered. This is purely a SQL question. All you need to know is how to join multiple tables in a SQL query, which would look something like this:
C#:
SELECT p.SomeParentColumn, c.SomeChildColumn
FROM Parent p
INNER JOIN Child c
ON p.ParentID = c.ParentID
You need to specify that the tables are joined on the primary key of the parent and foreign key of the child. Note that, if it's a one-to-many relationship, you're going to get multiple rows for the one student.
 
Back
Top Bottom