SQL LIKE query with variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can execute the following in SQL Query Analyzer:
Select productID, productName from Products where productName LIKE '%Spider%'
and get the results I was expecting.

I must be using the wrong syntax when I try to use a variable instead of a hard-coded search string in my ASP.NET code. I am not getting any results.
strSearch = "SELECT productID, productName from Products WHERE productName LIKE '%@searchphrase%'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add("@searchphrase", txtSearchPhrase.Text)

Thank you for any help you can give me,
Judy
 
try
strSearch = "SELECT productID, productName from Products WHERE productName
LIKE '@searchphrase'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add(%@searchphrase%, txtSearchPhrase.Text)

Put %...% in the parameters.
I think it work

Jean Jacques Serpoul

Judy Ward said:
I can execute the following in SQL Query Analyzer:
Select productID, productName from Products where productName LIKE '%Spider%'
and get the results I was expecting.

I must be using the wrong syntax when I try to use a variable instead of a
hard-coded search string in my ASP.NET code. I am not getting any results.
 
That exact syntax didn't work, but gave me the idea to try this:
cmdSearch.Parameters.Add("@searchphrase", "%" & txtSearchPhrase.Text & "%")

That worked. Thank you very much for responding!
Judy
 
Back
Top