SqlCommand cmd = new SqlCommand("select * from [Table] where Col1 like
%@astr% or Col2 like %@astr% or Col3 like %@astr%)", con);
cmd.Parameters.AddWithValue("@astr", st);
上面的命令出了什么问题? 我收到了“@astr附近”的错误消息。
答案 0 :(得分:1)
您必须在变量中指定通配符(%
),在查询中删除通配符。
st = string.Format("%{0}%", st); // in case your search string don't have wild characters..
SqlCommand cmd = new SqlCommand("select * from [Table] where Col1 like
%@astr% or Col2 like @astr or Col3 like @astr)", con);
cmd.Parameters.AddWithValue("@astr", st);
答案 1 :(得分:0)
搜索字符串周围的引号丢失,一个括号太多:
SqlCommand cmd = new SqlCommand(
@"select * from [Table]
where Col1 like '%' || @astr || '%'
or Col2 like '%' || @astr || '%'
or Col3 like '%' || @astr || '%'", con);
答案 2 :(得分:-1)
如果astr是一个字符串。你可以试试这个。
string astr = "ABC";
SqlCommand cmd = new SqlCommand("select * from [Table] where Col1 like %" + astr + "% or Col2 like %" + astr + "% or Col3 like %" + astr + "%", con);
cmd.Parameters.AddWithValue("@astr", st);