How do I do a like with parameters?

.net ado.net dapper database mysql

Question

Here is a made up query

"select * from tbl where name like '@foo%'"
...
cmd.addparam(foo, bar) //not actual code

I get 0 results. I tried changing the where to name like @foo and wrote bar+"%" however i suspect that is incorrect (it should escape %?) and i got 0 rows yet again.

How do I like with parameters? My code actually uses dapper.net with mysql as its database.

Accepted Answer

You need to construct your query as follows:

"select * from tbl where name like @foo"

then

cmd.addParam("@foo", bar + "%")


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why