0

When I run this command in MySQL:

SELECT * FROM v
WHERE v.firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%'

It returns 4 rows in the result set.

But when I run the same query using parameters in C# it returns only one.

SELECT * FROM v
WHERE v.firstname  LIKE ?word OR middlename LIKE ?word OR lastname 

cmd.Parameters.AddWithValue("word", '%'+key+'%');

I also tried '%' ?word '%' and adding the parameter (key) only, but this didn't work either.

How do I make this work?

4
  • How do you run your DbCommand? Show us the next statement after AddWithValue. Commented Jan 26, 2011 at 8:17
  • Did you try running the query hardcoded in your C# code? Commented Jan 26, 2011 at 8:17
  • don't worry it's work the problem is not in query elsewhere. the codewar tag i make for testing that how it's work.if anyone have problem destroy them i have no objection on them. Commented Jan 26, 2011 at 8:33
  • This question seems to have addressed the problem you're having. Commented Jan 26, 2011 at 9:00

3 Answers 3

0

Are you using something like:

cmd.ExecuteScalar()

in your code? It will return only 1 record. Try

cmd.ExecuteReader()

instead.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're missing "LIKE ?word" at then end of your second SQL statement.

Comments

0

WHERE v.firstname LIKE ?word OR middlename LIKE ?word OR lastname are you missing lastname LIKE ?word ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.