SQL LIKE query with variable

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
 
J

Jean Jacques Serpoul

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top