DevArt dotConnect for Oracle - how to do a WHERE LIKE clause with a parameter?
-
05-12-2019 - |
Frage
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?
Lösung
Try this solution:
cmd.CommandText =
"SELECT ID, COLUMN_A, COLUMN_B
FROM TABLE_A
WHERE COLUMN_A LIKE '%' || :SearchTerm || '%'";
cmd.Parameters.AddWithValue("SearchTerm", term);
Andere Tipps
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.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow