c# – 添加SqlParameter绑定LIKE’%@ x%'[复制]

c# – 添加SqlParameter绑定LIKE’%@ x%'[复制],第1张

概述参见英文答案 > Use of SqlParameter in SQL LIKE clause not working                                    3个 我在获取以下代码以正确添加SqlCommand参数@vendor时遇到问题.由于某种原因,传递的查询似乎总是: select TOP 500 * from [mike_db].[dbo].[na_pe_ 参见英文答案 > Use of SqlParameter in SQL LIKE clause not working3个
我在获取以下代码以正确添加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();                }
解决方法 我认为@vendor在您的查询中被视为文字,而不是参数.

尝试按如下方式定义查询:

string strquery =   "select top 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like '%' + @vendor + '%'";

然后添加如下参数:

cmd.Parameters.AdDWithValue("@vendor",search);
总结

以上是内存溢出为你收集整理的c# – 添加SqlParameter绑定LIKE’%@ x%'[复制]全部内容,希望文章能够帮你解决c# – 添加SqlParameter绑定LIKE’%@ x%'[复制]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249147.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存