parameters in search

  • Thread starter Thread starter Eirik Eldorsen
  • Start date Start date
E

Eirik Eldorsen

I'm trying to use parameters in a search, but I can't get it right.

Before I add parameters this works:
string cmd =
"SELECT * " +
"FROM MyTable" +
"WHERE Title LIKE '%" + keyword + "%'

Here my code with parameters that don't work:
string cmd =
"SELECT * " +
"FROM MyTable " +
"WHERE Title LIKE '%@Keyword%'";

command.CommandText = cmd;
command.Parameters.Add(new SqlParameter("@Keyword", keyword))


What am I doinig wrong?
 
Erik,

Change to

"WHERE Title LIKE @Keyword";

and include the '%'s in the parameter value:

command.Parameters.Add(new SqlParameter("@Keyword",
String.Format("%{0}%",keyword)));

Eliyahu
 
Thank you!


Eliyahu Goldin said:
Erik,

Change to

"WHERE Title LIKE @Keyword";

and include the '%'s in the parameter value:

command.Parameters.Add(new SqlParameter("@Keyword",
String.Format("%{0}%",keyword)));

Eliyahu
 
Back
Top