我在我的數據庫Student和Subject中有兩個表。我在StudentID(學生主鍵)和該主題的外鍵之間建立了關係。如何查詢與外鍵中特定名稱匹配的數據庫?
如何選擇所有的外鍵匹配的數據(StudentID爲主題),例如:2011017997顯示courseno,coursedecription,臺,日,開始時間等。
我想使用會話在下一頁顯示我的主題數據作爲Gridview。
這背後是我的aspx代碼:
string query = null;
string subjectquery = null;
int rowcounter = 0;
private DataSet studentData;
private DataSet subjectData;
private DataTable subjectTable;
protected void Button1_Click(object sender, EventArgs e)
{
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
//String decleration
string username = (this.UserName.Value);
string pass = (this.Password.Value);
//query database from sql server management studio for student
query = "select studentid,password,firstname,lastname,course,year from student";
//query database from sql server management studio for subject
subjectquery = "select CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room from subject";
//execute query for student
studentData = connector.ExecuteQuery(query);
//execute query for subject
subjectData = connector.ExecuteQuery(subjectquery);
for (;;)
{
//string decleration and getting each rows of the Student database
string userid = studentData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
string password = studentData.Tables[0].Rows[rowcounter]["Password"].ToString();
string firstname = studentData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
string lastname = studentData.Tables[0].Rows[rowcounter]["LastName"].ToString();
string course = studentData.Tables[0].Rows[rowcounter]["Course"].ToString();
string year = studentData.Tables[0].Rows[rowcounter]["Year"].ToString();
//get all subject data rows and store it into DataTable
subjectTable = subjectData.Tables[0];
//Username and Password are correct
if ((username == userid) && (pass == password))
{
Session["login"] = userid;
Session["firstname"] = firstname;
Session["lastname"] = lastname;
Session["course"] = course;
Session["year"] = year;
Session["Subjects"] = subjectTable;
Response.Redirect("StudentPage.aspx", true);
break;
}
//empty username and password
else if (((username == "") && (pass == "")) || ((userid == null) && (password == null)))
{
Response.Write("<script language=javascript>alert('Username and password must have input value. Try again');</script>");
break;
}
//username is invalid and password is correct ; username is correct and password is invalid
else if (((username != userid) && (pass == password)) || ((username == userid) && (pass != password)))
{
Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
break;
}
else
{
rowcounter++;
}
}
}
當程序顯示結果的主題的所有的數據顯示,但不能的studentID「2011017997」的數據。
我想我的問題是查詢,這樣的:
//query database from sql server management studio for subject
subjectquery = "select CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room from subject";
,這是我在會議顯示我的數據表:
//execute query for subject
subjectData = connector.ExecuteQuery(subjectquery);
//get all subject data rows and store it into DataTable
subjectTable = subjectData.Tables[0];
Session["Subjects"] = subjectTable;
顯示在另一個使用的GridView對象數據的代碼頁碼:
//Subject Data
subjectdata.DataSource = Session["Subjects"];
subjectdata.DataBind();
我該如何比較或僅僅顯示以下內容與以下StudentID = 2011017997匹配的cts?
這是一個很好的但它會顯示所有列名稱,包括studentid和teacherid。 我想到了這樣的事情: subjectquery =「select CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room from subject where studentid = 2011017997」; –
是的,選擇*表示顯示錶中的所有列,您可以將其更改爲您喜歡的。希望我的回答對你有幫助! – boyshot17