开发者

C# SQLite Parameterized Select Using LIKE

开发者 https://www.devze.com 2023-01-28 02:29 出处:网络
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 I use System.Data.SQLite in C#, it onl开发者_开发知识库y w

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 I use System.Data.SQLite in C#, it onl开发者_开发知识库y works with a literal, not a parameter, such as

string sel = "SELECT * FROM [TABLE] WHERE hostname LIKE '%@host%'";
...
command.Parameters.AddWithValue("@host", "myhostname");

This returns no results.


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%");


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

精彩评论

暂无评论...
验证码 换一张
取 消