简体   繁体   English

从数据集中检索数据

[英]Retrieve data from dataset

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

Here 'start' is a textbox value of a textbox in previous form ie form2. 这里的“开始”是以前形式即form2的文本框的文本框值。 I want to fetch StudentName and StudentID from StudentMaster where StudentID = start. 我想从StudentMaster =开始的StudentMaster中获取StudentName和StudentID。 The table is named 'StudentMaster'. 该表名为“ StudentMaster”。 Fill the dataset with StudentMaster. 用StudentMaster填充数据集。 Then I want to fetch Registration_No from Candidate_Registration where StudentID=start. 然后,我想从Candidate_Registration那里获取Student_ = start的Registration_No。 The table is named 'Candidate_Registration'. 该表名为“ Candidate_Registration”。 Fill the dataset with Candidate_Registration. 用Candidate_Registration填充数据集。 Now according to the 'Registration_No' that is fetched, I want to fetch 'CourseID' from 'Registered_Courses'. 现在根据获取的“ Registration_No”,我想从“ Registered_Courses”中获取“ CourseID”。 But, the problem is, how to access the fetched 'Registration_No' ie how to put it in the following query: if I can take the fetched Registration_No into a variable named 'reg_no' then, "Select CourseID from Registered_Courses where Registration_No="+ reg_no; 但是,问题是,如何访问获取的“ Registration_No”,即如何将其放入以下查询中:如果我可以将获取的Registration_No放入名为“ reg_no”的变量中,则“从Registered_Courses中选择CourseID,其中Registration_No =“ + reg_no;

For more understanding I mention the tables and the relationships.... 为了获得更多的理解,我提到了表格和关系。

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

ie finally I want to get the Course_Name for a particular StudentID. 即最后我想获得特定学生ID的Course_Name。

Can anyone please help me out. 谁能帮我一下。 Thanks in advance! 提前致谢!

Try this query : 试试这个查询:

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

Replace @MyId with your Id parameter and it gives you all the CourseNames for a StudentId. 将@MyId替换为您的Id参数,它会为您提供StudentId的所有课程名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM