How to use parameter with LIKE in Sql Server Compact Edition

Posted by Colin on Stack Overflow See other posts from Stack Overflow or by Colin
Published on 2009-12-16T17:27:04Z Indexed on 2010/05/23 2:20 UTC
Read the original article Hit count: 354

Filed under:
|
|
|

I'm trying to parameterise a search query that uses the LIKE keyword with a wildcard. The original sql has dynamic sql like this:

"AND JOB_POSTCODE LIKE '" + isPostCode + "%' "

So I've tried this instead, but I get a FormatException:

"AND JOB_POSTCODE LIKE @postcode + '%' "

Edit: I guess the FormatException isn't going to be coming from Sql Server CE, so as requested, here is how I set the parameter in my C# code. The parameter is set in code like this:

command.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = isPostCode;

I also tried:

"AND JOB_POSTCODE LIKE @postcode"

with

command.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = isPostCode + "%";

but that doesn't return any results. Can anyone advise how to use parameters in this search sql?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET