简体   繁体   English

我如何在C#中使用like语句

[英]how i can use like statement in c#

i am adding parameter by 我通过添加参数

qry = qry.Replace("{criteria}", "info.abc LIKE '%?val%'");

command not worked if i removed ' ' from the command then it give a error how i can search the table in c# 如果我从命令中删除了' '命令,则该命令不起作用,那么它给出了一个错误,我该如何在C#中搜索表格

As per the syntax of TSQL - Like you require to put search value between ' ' 按照TSQL的语法-就像您需要在''之间放置搜索值

Example : 范例:

WHERE title LIKE '%computer%'

syntax 句法

match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

Another way to do this which is more explicit - and in my opinion more readable because it avoids the crockety parts of the SQL syntax: 另一种执行此操作的方法是更明确的-在我看来,这更易读,因为它避免了SQL语法的复杂部分:

SqlDataReader r = new SqlCommand("SELECT * FROM the_table").ExecuteReader();
object[] values = new object[5000];
r.GetValues(values);
foreach (string value in values)
    if (value.Length > 4)
        if (value.Contains("val"))
            new SqlCommand("UPDATE the_table SET value = 'newValue' WHERE "+
                           "value = '"+value+"'").ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM