从数据集中检索数据

时间:2010-12-20 15:37:16

标签: c# winforms ado.net

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");

这里'start'是以前形式的文本框的文本框值,即form2。 我想从StudentMaster中获取StudentID和StudentID,其中StudentID = start。 该表名为“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。

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

1 个答案:

答案 0 :(得分: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

将@MyId替换为您的Id参数,它会为您提供StudentId的所有CourseNames。