[英]How to use like operator with %?
我正在创建一个搜索栏,并且很难为此构建正确的查询。 这是我的代码:
SqlCommand command1 = new SqlCommand(
"Select * from tbl_customer where customer_name like '%''"+ textBox1.Text +"''%' ",
MySqlConnection);
SqlCommand command1 = new SqlCommand("Select * from tbl_customer where customer_name like @search_value", MySqlConnection);
command1.Parameters.AddWithValue("@search_value","%" + textBox1.Text + "%");
您要添加太多。
SqlCommand command1 = new SqlCommand(
"Select * from tbl_customer where customer_name like '%"+ textBox1.Text +"%' ",
MySqlConnection);
请注意,我已删除了第一个%之后最后一个%之前的多余的。
但是,您应该注意SQL注入并使用参数,而不要直接将控制值添加到查询中。
SqlCommand command1 = new SqlCommand(
"Select * from table-name where column-name like '%"+ textboxid.Text +"%' ",
MySqlConnection);
如果您制作一个示例程序,那么就可以了,但是,如果您正在寻找专业用途的软件或网站,则不要使用此方法。 检查sql注入,因为在这里您直接在查询中添加控件值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.