参见英文答案 >
Use of SqlParameter in SQL LIKE clause not working3个
我在获取以下代码以正确添加sqlCommand参数@vendor时遇到问题.由于某种原因,传递的查询似乎总是:
我在获取以下代码以正确添加sqlCommand参数@vendor时遇到问题.由于某种原因,传递的查询似乎总是:
select TOP 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like '%@vendor%';
如果我像这样设置查询,它可以工作,但我知道这是不好的做法:
string strQuery = "select TOP 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like '%"+txt_search.Text.ToString()+"%';";
这是代码:
protected void Search_Click(object sender,EventArgs e) { string search = txt_search.Text.ToString(); String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["mike_db"].ConnectionString; sqlConnection con = new sqlConnection(strConnString); con.Open(); string strQuery = "select TOP 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like '%@vendor%';"; cmd = new sqlCommand(strQuery,con); cmd.Parameters.AddWithValue("vendor",search); txt_search.Text = string.Empty; DataSet ds = new DataSet(); da = new sqlDataAdapter(cmd); da.Fill(ds); My_Repeater.DataSource = ds; My_Repeater.DataBind(); con.Close(); }