使用参数在asp中传递SQL查询时出错

时间:2012-07-04 19:14:14

标签: c# asp.net .net sql-server-2008-r2

我正在使用此代码,当我运行代码时出现错误。

  

where where子句附近的语法不正确。

这是我的C#代码:

SqlConnection con = new SqlConnection("Data Source=ANURAG-PC;Initial Catalog=dbPortal;Persist Security Info=True;User ID=yyyy;Password=xxxxx");

protected void Page_Load(object sender, EventArgs e) 
{
   if (IsPostBack == false)
   {
       string s = Request.QueryString["cat"];
       string s1 = Request.QueryString["sub"];

       SqlCommand cmd = new SqlCommand("Select * from Architect where where SubCategory1 = @sub1",con);
       cmd.Parameters.AddWithValue("@sub1", s1);

       con.Open();

       using (SqlDataReader reader = cmd.ExecuteReader())
       {
           DataTable dat = new DataTable("tab");
           dat.Load(reader);
           DataGrid1.DataSource = dat;
           DataGrid1.DataBind();
       }
    }
}

它有什么不对吗?

2 个答案:

答案 0 :(得分:2)

查看查询 - 您有where where。应该只有一个where

SqlCommand cmd = 
    new SqlCommand("Select * from Architect where where SubCategory1=@sub1",con);

应该是:

SqlCommand cmd = 
    new SqlCommand("Select * from Architect where SubCategory1=@sub1",con);

答案 1 :(得分:1)

你好正确删除哪里,你有两个

SqlCommand cmd = new SqlCommand("Select * from Architect where where SubCategory1=@sub1",con);

-> SqlCommand cmd = new SqlCommand("Select * from Architect where SubCategory1=@sub1",con);