我創(chuàng)建了 3 個(gè)下拉列表,我想按學(xué)生姓名、父親姓名或序列號(hào)進(jìn)行搜索。當(dāng)我選擇學(xué)生姓名或父親姓名并寫下姓名時(shí),它會(huì)顯示數(shù)據(jù)。但是當(dāng)我選擇“按序列號(hào)”搜索時(shí),出現(xiàn)錯(cuò)誤:已經(jīng)有一個(gè)DataReader與此命令關(guān)聯(lián)的打開,必須先關(guān)閉。錯(cuò)誤代碼搜索截圖我試圖關(guān)閉 dataReader 但它不起作用。protected void btnSearch_Click(object sender, EventArgs e){ string QueryStudent = "Select SerialNumber as [Serial No.], Name as [Student Name], FatherName as Father,Phone,Course, AdmissionDate as[Admission date], Status from tblStudents where Name= '" + txtSearch.Text + "'"; string QueryFather = "Select SerialNumber as [Serial No.], Name as [Student Name], FatherName as Father,Phone,Course, AdmissionDate as[Admission date], Status from tblStudents where FatherName= '" + txtSearch.Text + "'"; string QuerySerial = "Select SerialNumber as [Serial No.], Name as [Student Name], FatherName as Father,Phone,Course, AdmissionDate as[Admission date], Status from tblStudents where SerialNumber= '" + txtSearch.Text + "'"; con.Open(); using (var sqlCommand = new SqlCommand(QueryStudent, con)) using (var sqlCommand2 = new SqlCommand(QueryFather, con)) using (var sqlCommand3 = new SqlCommand(QuerySerial, con)) { SqlDataReader reader = sqlCommand.ExecuteReader(); if (DropDownList.SelectedItem.Value == "By Student Name") { if (!reader.HasRows) { GrindSearch.Visible = false; lblmsg.Visible = true; lblmsg.Text = "No Record found"; }
2 回答

犯罪嫌疑人X
TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以通過(guò)將 MultipleActiveResultSets=true 添加到連接字符串的提供程序部分來(lái)解決此問(wèn)題。

冉冉說(shuō)
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
該"By Serial Number"
子句不以相同的reader.Close();
開頭"By Father Name"
。
但更一般地說(shuō),您對(duì)該讀者的管理是脆弱的。
它應(yīng)該在一個(gè)using(...) {...}
塊中管理。
而且,相關(guān)的,算法結(jié)構(gòu)看起來(lái)遠(yuǎn)非高效。當(dāng)DropDownList.SelectedItem.Value == "By Serial Number"
您已經(jīng)執(zhí)行了 QueryStudent 時(shí),永遠(yuǎn)不要使用它的結(jié)果。
- 2 回答
- 0 關(guān)注
- 100 瀏覽
添加回答
舉報(bào)
0/150
提交
取消