使用LIKE的C#SQLite参数化选择 [英] C# SQLite Parameterized Select Using LIKE
本文介绍了使用LIKE的C#SQLite参数化选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行
SELECT * FROM [TABLE] WHERE hostname LIKE '%myhostname%';
这在普通SQL中工作正常,但是当我在C#中使用System.Data.SQLite时,它仅适用于文字,而不是参数,例如
This works fine in plain SQL, but when I use System.Data.SQLite in C#, it only works with a literal, not a parameter, such as
string sel = "SELECT * FROM [TABLE] WHERE hostname LIKE '%@host%'";
...
command.Parameters.AddWithValue("@host", "myhostname");
这不会返回任何结果.
推荐答案
您不能这样做.参数必须是完整值-不仅仅是在SQL中的字符串替换.您可以改为:
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%");
这篇关于使用LIKE的C#SQLite参数化选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文