无效的列名搜索错误

时间:2014-02-28 10:58:30

标签: c# asp.net sql-server-express

我正在尝试获取搜索查询 它在asp.net c#中请帮我搜索查询。

protected void btnreg_Click(object sender, EventArgs e)
    {
    string search = query.Text;
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM recipe WHERE  search LIKE '%' + nor + '%' OR recipe LIKE '%' + search + '%'  OR ingredients LIKE '%' + search + '%'  OR type_of_food LIKE '%' + search + '%' OR type_of_meal LIKE '%' + search + '%' ", con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    repeter.DataSource = dt;
    repeter.DataBind();
    }}

4 个答案:

答案 0 :(得分:0)

我想'也不是'是表格中的列名称。请尝试以下操作: -

protected void btnreg_Click(object sender, EventArgs e)
   {
    string search = query.Text;
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM recipe WHERE  nor LIKE '%'" + search + "'%' OR recipe LIKE '%' "+ search + "'%'  OR ingredients LIKE '%' "+ search + "'%'  OR type_of_food LIKE '%' "+ search + "'%' OR type_of_meal LIKE '%' "+ search +" '%' ", con);

    DataTable dt = new DataTable();
    da.Fill(dt);
    repeter.DataSource = dt;
    repeter.DataBind();

}
}

实际上你不能将#var;双引号"中的变量搜索括起来。标签 。 希望这会对你有所帮助。

干杯!

答案 1 :(得分:0)

为什么不使用参数化查询!

E.g

var command = "SELECT * FROM recipe WHERE  recipe LIKE '% @Receipe %'";

var cmd= new SqlCommand(command , yourconnetion);

cmd.Parameters["@Receipe "].Value =query.Text;

OR

cmd.Parameters.AddWithValue("@Receipe ",query.Text);

详细了解How and Why to Use Parameterized Queries

答案 2 :(得分:0)

最后我得到了正确的一个,就像这样 虽然感谢你的帮助。

protected void btnreg_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM recipe WHERE  nor LIKE '%" + query.Text + "%' OR recipe LIKE '%" + query.Text + "%'  OR ingredients LIKE '%" + query.Text + "%'  OR type_of_food LIKE '%" + query.Text + "%' OR type_of_meal LIKE '%" + query.Text + "%' ", con);

    DataTable dt = new DataTable();
    da.Fill(dt);
    repeter.DataSource = dt;
    repeter.DataBind();

}

答案 3 :(得分:0)

Use `Parametarized` and `Using{}` statement to auto dispose and close connection


using( SqlConnection objConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True"))
            {
                    objConnection.Open();

                    try
                    {

                        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM recipe WHERE  nor LIKE '% @query %'" , con))
                       DataTable dt= new DataTable();
                        da.SelectCommand.Parameters.AddWithValue("@query",query.Text); 
                       da.Fill(dt);


                    }
                    catch(System.Data.SqlClient.SqlException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }



                }