C#构造参数查询SQL - LIKE% [英] C# constructing parameter query SQL - LIKE %

查看:266
本文介绍了C#构造参数查询SQL - LIKE%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立SQL在C#中的参数查询,这将包含 LIKE %% 命令查询。

I am trying to build SQL for a parameter query in C# for a query which will contain the LIKE %% command.

下面是我想acheive(请注意,该数据库是火鸟)

Here is what I am trying to acheive (please note that the database is Firebird)

var SQL = string.format("SELECT * FROM {0} WHERE {1} LIKE '%?%'", TABLE, NAME);
 cmd.Parameters.AddWithValue(NAME, "JOHN");

现在我已经尝试了每一个置换来获取参数的工作,我都试过;

Now I have tried every single permutation to get the parameter to work, I have tried;


  • 添加字符参数,

cmd.Parameters.AddWithValue(NAME, "%" + "JOHN" + "%");


  • or

    cmd.Parameters.AddWithValue(NAME, "'%" + "JOHN" + "%'");
    


  • 我似乎无法得到这个工作,我怎么可以用一个参数为LIKE查询工作。

    I cannot seem to get this to work, how can I use a parameter for the LIKE query to work.

    建议,欢迎!

    推荐答案

    您不能有一个字符串中的查询文本的内部参数。使整个值的参数,以及通配符添加到字符串

    You can't have parameters inside of a string literal in the query. Make the entire value the parameter, and add the wildcards to the string:

    var SQL = string.format("SELECT * FROM {0} WHERE {1} LIKE ?", TABLE, NAME);
    Cmd.Parameters.AddWithValue(NAME, "%" + "JOHN" + "%");
    

    这篇关于C#构造参数查询SQL - LIKE%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆