?
Solved

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

Posted on 2011-09-25
8
Medium Priority
?
563 Views
Last Modified: 2012-06-27
Hello,

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?

Thanks
0
Comment
Question by:prinsbj
8 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36594925
select * from products where ProductDescription like '%'+SearchText+'%'
0
 

Author Comment

by:prinsbj
ID: 36594951
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?

Thanks

0
 
LVL 15

Expert Comment

by:Eyal
ID: 36594955
Now I understood what is your problem.

can it be exact search or it must include %%?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:prinsbj
ID: 36594962
It must incl %%.

0
 
LVL 15

Expert Comment

by:Eyal
ID: 36594980
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
0
 

Author Comment

by:prinsbj
ID: 36595040
Could you explain met optie 3 more. I've use dataset in visual studio.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 36595271
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:
SELECT *
FROM YourTableName
WHERE CONTAINS(YourColumn, '"Keyword1 OR Keyword2 OR Keyword3 OR ..."')
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36595449
Try using a query like
(Untested)

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"
 Next
 Query = Query.TrimEnd("OR ProductDescription".ToCharArray())
Else
 Query &= "Like '%" & txtSearch.Text & "%'"
End If

'Execute query
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question