none
关于必须声明变量 '@ST2',的问题! RRS feed

  • 问题

  • 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();
    
    
    
            }
    
    
    
                                     
    
    
    
    }
    
    
    
    
    必须声明变量 '@ST2',不知道错在哪了?
    2010年2月4日 8:08

答案

全部回复