DevArt dotConnect for Oracle - 如何使用参数执行 WHERE LIKE 子句?
如何使用 DevArt dotConnect for Oracle 库?
理想情况下,对 SearchTerm 使用 CommandParameter
吗?
我尝试了以下方法,但没有感到高兴:
cmd.CommandText =
"SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
WHERE COLUMN_A LIKE :SearchTerm";
我已经可以使用它了:
cmd.CommandText =
"SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
WHERE COLUMN_A LIKE :SearchTerm";
cmd.Parameters.AddWithValue("SearchTerm", "%" + term.Replace('%', ' ') + "%");
但我不满意用 %
符号包含该术语 - 有正确或更好的方法吗?
How do I do a WHERE COLUMN LIKE '%SearchTerm%'
with the DevArt dotConnect for Oracle libraries?
Ideally using a CommandParameter
for the SearchTerm?
I've tried the following with no joy:
cmd.CommandText =
"SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
WHERE COLUMN_A LIKE :SearchTerm";
I've got it working with:
cmd.CommandText =
"SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
WHERE COLUMN_A LIKE :SearchTerm";
cmd.Parameters.AddWithValue("SearchTerm", "%" + term.Replace('%', ' ') + "%");
But I'm not happy with encasing the term with %
signs - is there a correct or better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个解决方案:
Try this solution:
Oracle 本身不喜欢“Where column LIKE %:searchterm%”,因此如果您需要其中的通配符,则需要将它们添加为参数的一部分。
您可以通过在所需列上设置全文索引来避免这种情况进行搜索,然后使用 CONTAINS 代替,这不需要通配符。它也是一种更强大的搜索方法,但设置起来更复杂。
Oracle itself doesn't like "Where column LIKE %:searchterm%", so if you need the wildcards in there then they need to be added as part of the parameter.
You could avoid it by setting up a full text index on the column you want to search and then use CONTAINS instead, which doesn't require the wildcards. It's also a more powerful search method, but more complex to set up.