积极答复者
必须声明变量 '@ST2'

问题
-
protected void btnResearch_Click(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; DataSet ds = new DataSet(); SqlConnection conn = new SqlConnection(connStr); if (conn.State.ToString() == "Closed") conn.Open(); string SchoolStr = "", DepartStr = "", SpecialStr = "", GradeStr=""; Session["SearchStr"] = @"select ST2.*,DT.DepartID,DT.DepartName,ST.SpecialtyID,ST.SpecialtyName,GT.* from SchoolTable as ST2 ,DepartTable as DT,SpecialtyTable as ST,GradeTable as GT where ST2.SchoolID=DT.SchoolID and DT.DepartID=ST.DepartID"; if (DropDownList1.SelectedValue.ToString() != "") { SchoolStr = " and ST2.SchoolID=@ST2.SchoolID"; Session["SearchStr"] = Session["SearchStr"] + SchoolStr; if (DropDownList2.SelectedValue.ToString() != "") { DepartStr = "and DT.DepartID=@DT.DepartID"; Session["SearchStr"] = Session["SearchStr"] + DepartStr; if (DropDownList3.SelectedValue.ToString() != "") { SpecialStr = "and ST.SpecialtyID=@ST.SpecialtyID"; Session["SearchStr"] = Session["SearchStr"] + SpecialStr; if (DropDownList4.SelectedValue.ToString() != "") { GradeStr = "and GT.GradeID=@GT.GradeID"; Session["SearchStr"] = Session["SearchStr"] + GradeStr; } } } } string SearchStr = Session["SearchStr"].ToString(); try { SqlCommand comm = new SqlCommand(SearchStr, conn); comm.Parameters.AddWithValue("@ST2.SchoolID", this.DropDownList1.SelectedValue); comm.Parameters.AddWithValue("@DT.DepartID", this.DropDownList2.SelectedValue); comm.Parameters.AddWithValue("@ST.SpecialtyID", this.DropDownList3.SelectedValue); comm.Parameters.AddWithValue("@GT.GradeID", this.DropDownList4.SelectedValue); comm.ExecuteNonQuery(); comm.Dispose(); SqlDataAdapter da = new SqlDataAdapter(SearchStr, conn); da.Fill(ds); if (conn.State.ToString() == "Open") conn.Close(); GridView1.DataSource = ds; GridView1.DataBind(); } catch (Exception ex) { Response.Write("数据库错误,错误原因:" + ex.Message); Response.End(); } finally { if (conn.State.ToString() == "Open") conn.Close(); } }
- 已编辑 Smiling008 2010年2月5日 2:16
- 已移动 Sheng Jiang 蒋晟Moderator 2010年2月12日 0:06 System.Data (发件人:ASP.NET 与 AJAX)
答案
-
-
两位前辈,我按你们的方法试了一下,结果出现了这个错误:数据库错误,错误原因:必须声明变量 '@STSchoolIDand'
会不会是我写的程序出现了问题!
如果报这样的错误,就说明你在 Sql 字符串中声明了 @STSchoolIDand 参数,但又没给对应的 Command 的 Parameters 添加 @STSchoolIDand 的声明。
知识改变命运,奋斗成就人生!- 已标记为答案 肖小勇Moderator 2010年2月5日 1:04
全部回复
-
-
两位前辈,我按你们的方法试了一下,结果出现了这个错误:数据库错误,错误原因:必须声明变量 '@STSchoolIDand'
会不会是我写的程序出现了问题!
comm.Parameters.AddWithValue("@ST2.SchoolID", this.DropDownList1.SelectedValue);
comm.Parameters.AddWithValue("@DT.DepartID", this.DropDownList2.SelectedValue);
comm.Parameters.AddWithValue("@ST.SpecialtyID", this.DropDownList3.SelectedValue);
comm.Parameters.AddWithValue("@GT.GradeID", this.DropDownList4.SelectedValue);
这些确定够了吗 或者改了没 -
两位前辈,我按你们的方法试了一下,结果出现了这个错误:数据库错误,错误原因:必须声明变量 '@STSchoolIDand'
会不会是我写的程序出现了问题!
comm.Parameters.AddWithValue("@ST2.SchoolID", this.DropDownList1.SelectedValue);
comm.Parameters.AddWithValue("@DT.DepartID", this.DropDownList2.SelectedValue);
comm.Parameters.AddWithValue("@ST.SpecialtyID", this.DropDownList3.SelectedValue);
comm.Parameters.AddWithValue("@GT.GradeID", this.DropDownList4.SelectedValue);
这些确定够了吗 或者改了没
我改了,还是出现了上述的错误,所以我又再次使用了SQL的拼串方法来解决了上述问题! -
两位前辈,我按你们的方法试了一下,结果出现了这个错误:数据库错误,错误原因:必须声明变量 '@STSchoolIDand'
会不会是我写的程序出现了问题!
如果报这样的错误,就说明你在 Sql 字符串中声明了 @STSchoolIDand 参数,但又没给对应的 Command 的 Parameters 添加 @STSchoolIDand 的声明。
知识改变命运,奋斗成就人生!- 已标记为答案 肖小勇Moderator 2010年2月5日 1:04