Need SQL query in VB.NET that search in column to Multiple Keywords


I've a table with 3 fields

ProdictID - ProductDescription - Price

Now I want to build a dynamic seach option. I've a textbox to enter the keywords in.

Now when i click on my search button I should get the result of the keywords I've entered.

Could somebody help me with this?

Who is Participating?
Anthony PerkinsConnect With a Mentor Commented:
You also should look into Full-Text Search, especially if the table is large.  Once Full-Text Search is setup and the index populated your query becomes as simple as:
FROM YourTableName
WHERE CONTAINS(YourColumn, '"Keyword1 OR Keyword2 OR Keyword3 OR ..."')
select * from products where ProductDescription like '%'+SearchText+'%'
prinsbjAuthor Commented:
That's right but when i put in multiple keywords the it looks to:

Sample: Keyword_1 Keyword_2 Keyword_3

In your code it will look to "Keyword_1 Keyword_2 Keyword_3"

I've to look to the keywords separete. "Keyword_1" And "Keyword_2" And "Keyword_3"

Or if I put in 2 keywords than for 2.

Hope you know what I mean?


Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Now I understood what is your problem.

can it be exact search or it must include %%?
prinsbjAuthor Commented:
It must incl %%.

you can do it in 3 ways

1) execute_sql (@str) where @str is a query you build (I don't like this solution)
2) use data base cursor
3) use linq in the program :

List<string> uids = new List<string>( arrayofields.split(' ') );

from xx in table
where uids.Contains( xx.ToString() )
select xx
prinsbjAuthor Commented:
Could you explain met optie 3 more. I've use dataset in visual studio.
Try using a query like

Dim Query As String = "Select * from tblProducts Where ProductDescription"
If txtSearch.Text.Contains(" ") Then
 Dim Words As String() = txtSearch.Text.Split(" ")
 For Each Word in Words
     Query &= " Like '%" & word & "%' OR ProductDescription"
 Query = Query.TrimEnd("OR ProductDescription".ToCharArray())
 Query &= "Like '%" & txtSearch.Text & "%'"
End If

'Execute query
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.