MDL
MDL

Reputation: 271

Sql query for search program in vb.net

Trying to figure out the right way to query a MySql database and find a user defined variable from a Textbox.Text property. I'm not sure what to put for the where clause in order to search every cell of every row for a match. I have looked at coalesce and contains, but neither are really making sense to me and I'm not sure if they are my best option. I'm thinking that the query string will look something like this:

SELECT * FROM table WHERE (I dont know) = '" & searchBarTextVariable & "'

Upvotes: 0

Views: 1229

Answers (2)

Kibbee
Kibbee

Reputation: 66132

There is no specific syntax to search all columns, you have to list them out individually. You can write a query as follows.

SELECT * FROM table WHERE 
Field1 = '" & searchBarTextVariable & "'
OR Field2 = '" & searchBarTextVariable & "'
OR Field3 = '" & searchBarTextVariable & "'
OR Field4 = '" & searchBarTextVariable & "'
...

You should only list fields that are actually text, or be careful of conversion errors. Also watch out for SQL injection.

Upvotes: 0

Yuck
Yuck

Reputation: 50855

Without a dynamic solution you will have to specify each column you'd like to search. This is a pretty fundamental part of working with a relational database; MySQL is no exception.

As mentioned in a @jadarnel27's comment, you should use parameters instead. Something like:

SELECT *
FROM table
WHERE someColumnA = @searchBarTextVariable
   OR someColumnB = @searchBarTextVariable
   OR someColumnC = @searchBarTextVariable
   OR someColumnD = @searchBarTextVariable

Upvotes: 1

Related Questions