Wildcard "%" not working

  • Thread starter Thread starter MB
  • Start date Start date
M

MB

Hi,
I am using a datagrid and the datagrid is filled through the SQL
query. The SQL query searches for the match in the database. The SQL
Query that i am writing is as follows

Dim selprod As String = "SELECT ProductID, ProductName FROM Products
WHERE ProductName LIKE '%prod%'"

but it is not working.
If I use the query this way(as given below) it works but I want to use
wildcard so that it searches thoroughly and provide with me with all
the records

Dim selprod As String = "SELECT ProductID, ProductName FROM Products
WHERE ProductName LIKE '" + prod + "'"

If i use LIKE '" +%prod%+ "'" or something sort of like this it doesnt
work either... any idea how it will work.

P.S My database is in SQL Server

Thanks in advance
MB
 
Hi,

Dim strConn As String
Dim conn As SqlConnection

strConn = "Server = (local);"
strConn += "Database = NorthWind;"
strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

Dim cmd As SqlCommand
Dim strProduct As String = "chef"
Dim strSQL As String
strSQL = String.Format("Select * from Products Where Productname
LIKE '%{0}%'", strProduct)
cmd = New SqlCommand(strSQL, conn)

conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While dr.Read
Console.WriteLine(dr.Item("productname"))
Loop
End Sub

Ken
 
* (e-mail address removed) (MB) scripsit:
I am using a datagrid and the datagrid is filled through the SQL
query. The SQL query searches for the match in the database. The SQL
Query that i am writing is as follows

Dim selprod As String = "SELECT ProductID, ProductName FROM Products
WHERE ProductName LIKE '%prod%'"

but it is not working.
If I use the query this way(as given below) it works but I want to use
wildcard so that it searches thoroughly and provide with me with all
the records

Dim selprod As String = "SELECT ProductID, ProductName FROM Products
WHERE ProductName LIKE '" + prod + "'"

If i use LIKE '" +%prod%+ "'" or something sort of like this it doesnt
work either... any idea how it will work.

Your question is 0 percent related to VB.NET. Please post to the
appropriate group in future:

<URL:
Web interface:

<URL:http://msdn.microsoft.com/newsgroups/?dg=microsoft.public.dotnet.framework.adonet>

Thank you for your cooperation!
 
Back
Top