简体   繁体   English

在vb.net中搜索程序的SQL查询

[英]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. 试图找出正确的方法来查询MySql数据库并从Textbox.Text属性中找到用户定义的变量。 I'm not sure what to put for the where clause in order to search every cell of every row for a match. 我不确定在where子句中要放什么,以便在每行的每个单元格中搜索匹配项。 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 & "'

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. MySQL也不例外。

As mentioned in a @jadarnel27's comment, you should use parameters instead. 如@ jadarnel27的注释中所述,您应该改用参数。 Something like: 就像是:

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

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. 还要注意SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM