gpt4 book ai didi

c# - 添加SqlParameter绑定(bind)LIKE '%@x%'

转载 作者:太空狗 更新时间:2023-10-29 21:08:41 26 4
gpt4 key购买 nike

<分区>

我在获取以下代码以正确添加 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();
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com