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?

Jeff Atwood
  • 60,897
  • 45
  • 146
  • 152
  • How do you run your DbCommand? Show us the next statement after AddWithValue. – Al Kepp Jan 26 '11 at 08:17
  • Did you try running the query hardcoded in your C# code? – Abdel Raoof Jan 26 '11 at 08: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. –  Jan 26 '11 at 08:33
  • This [question](http://stackoverflow.com/questions/664314/c-constructing-paramater-query-sql-like) seems to have addressed the problem you're having. – Chris W Jan 26 '11 at 09:00

3 Answers3

0

Are you using something like:

cmd.ExecuteScalar()

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

cmd.ExecuteReader()

instead.

Cheng Chen
  • 39,413
  • 15
  • 105
  • 159
0

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

Brent Muir
  • 11
  • 1
0

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

Harish
  • 2,235
  • 4
  • 21
  • 28