How to use parameter with LIKE in Sql Server Compact Edition
- by Colin
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?