2010-12-20 66 views
0
SqlDataAdapter da = new SqlDataAdapter("Select StudentID,StudentName from StudentMaster where StudentID = '" + start + "'", conn);    
DataSet ds = new DataSet(); 
da.Fill(ds,"StudentMaster"); 
SqlDataAdapter db = new SqlDataAdapter("Select Registration_No from Candidate_Registration where StudentID='" + start + "'", conn); 
db.Fill(ds, "Candidate_Registration"); 

这里'开始'是以前形式的文本框的文本框值,即form2。 我想从StudentMaster中获取StudentID和StudentID,其中StudentID =开始。 该表名为'StudentMaster'。 用StudentMaster填充数据集。 然后,我想从Candidate_Registration获取Registration_No,其中StudentID = start。 该表名为'Candidate_Registration'。 用Candidate_Registration填充数据集。 现在根据提取的'Registration_No',我想从'Registered_Courses'中获取'CourseID'。 但问题是,如何访问提取的'Registration_No',即如何将它放入以下查询中: 如果我可以将提取的Registration_No带入名为'reg_no'的变量,则 “从Registered_Courses中选择CourseID,其中Registration_No =“+ reg_no;从数据集检索数据

更多的了解我提到的表和关系....

StudentMaster 
------------- 
StudentID Primary key, 
StudentName 

Candidate_Registration 
---------------------- 
Registration_No Foreign key, 
ExamID Foreign key, 
StudentID Foreign key, 
Seat_No, 
Primary key(Registration_No,ExamID) 

Registered_Courses 
------------------ 
Registration_No Primary key, 
ExamID Foreign key, 
CourseID Foreign key, 

Course_Master 
------------- 
CourseID Primary key, 
Course_Name, 
Description 

即最后我想对特定StudentID的COURSE_NAME。

任何人都可以请帮助我。提前致谢!

+2

您应该真的使用参数化查询 – Pabuc 2010-12-20 15:41:06

回答

1

尝试此查询:

Select StudentMaster.StudentId, Course_Master.Course_Name from StudentMaster 
INNER JOIN Candidate_Registration 
ON Candidate_Registration.StudentId = StudentMaster.StudentId 
INNER JOIN Registered_Courses 
ON Registered_Courses.Registration_No = Candidate_Registration.Registration_No AND Registered_Courses.ExamID = Candidate_Registration.ExamID 
INNER JOIN Course_Master 
ON Course_Master.CourseID = Registered_Courses.CourseID 
WHERE StudentMaster.StudentId = @MyId 

与您的ID参数替换@MyId和它给你一个StudentId所有CourseNames。

+0

谢谢4 d答案,但是当我应用它时,在运行时遇到错误“Ambiguous Column StudentID”da.Fill(ds,“StudentMaster”); – Shiv 2010-12-20 17:27:21

+0

@ user463493:编辑了查询,WHERE子句中缺少一个StudentMaster :) – LaGrandMere 2010-12-20 17:32:15