C# SQLite Parameterized Select Using LIKE

前端 未结 2 1619
眼角桃花
眼角桃花 2020-12-16 13:08

I am trying to do an SQL query such as

SELECT * FROM [TABLE] WHERE hostname LIKE \'%myhostname%\';

This works fine in plain SQL, but when

相关标签:
2条回答
  • 2020-12-16 13:39

    Easiest way to do this is to use '||'

    Use :

    const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName || '%' ";
    

    Instead Of:

    const string qry = "SELECT SiteNum FROM WorkTable WHERE WTName LIKE @wtName%";
    

    I have already answered here

    0 讨论(0)
  • 2020-12-16 13:45

    You can't do that. The parameters must be complete values - it's not just a string substitution into the SQL. You could do this instead:

    string sel = "SELECT * FROM [TABLE] WHERE hostname LIKE @host";
    ...
    command.Parameters.AddWithValue("@host", "%myhostname%");
    
    0 讨论(0)
提交回复
热议问题