与不同的字符串输入相比,如何使用 vb.net 在 mysql 中使用 LIKE 子句进行查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28710233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:50:12  来源:igfitidea点击:

How to query using LIKE clause in mysql using vb.net comparing to a varied string input?

mysqlsqlvb.netsql-likeclause

提问by MAC

I've search and tried a lot of procedure but did not work. CREATE PROCEDURE did not work, SET @variable is not acceptable and a couple more codes. This is my last "query" code that still didn't work.

我已经搜索并尝试了很多程序,但没有奏效。CREATE PROCEDURE 不起作用,SET @variable 是不可接受的,还有一些代码。这是我最后一个仍然无效的“查询”代码。

qry = "select * from employeefile where empfname LIKE '%''" + input + "''%'"

empfname is a name of an employee from table employeefile that possibly consists of three words. The input could be the first word, second word, third word or the entire words.

empfname 是表employeefile 中员工的姓名,可能由三个单词组成。输入可以是第一个词、第二个词、第三个词或整个词。

when i tried to input any word within the name, the program will still prompt, "no records found." when i tried to change the query into

当我尝试在名称中输入任何单词时,程序仍会提示“未找到记录”。当我尝试将查询更改为

qry = "select * from employeefile where empfname LIKE '%existingname%'"

and my input is "existingname", the program runs just as i want it to.

我的输入是“existingname”,程序按照我想要的方式运行。

This code is one of those that i seached but still didn't work.

这段代码是我搜索过但仍然无效的代码之一。

T-SQL and the WHERE LIKE %Parameter% clause

T-SQL 和 WHERE LIKE %Parameter% 子句

How to use like clause in MySQL 5.0 Statement

如何在 MySQL 5.0 Statement 中使用 like 子句

T-SQL and the WHERE LIKE %Parameter% clause

T-SQL 和 WHERE LIKE %Parameter% 子句

The problem here is when i use a variable... i probably get the wrong way of using it into query. Please help me. I am new here by the way.

这里的问题是当我使用一个变量时......我可能会以错误的方式将它用于查询。请帮我。顺便说一下,我是新来的。

采纳答案by MAC

i got the answer. it turns out that i just overdid the single quote. it must be written this way:

我得到了答案。事实证明,我只是过度使用了单引号。必须这样写:

qry = "select * from employeefile where empfname LIKE '%" + input + "%'"

回答by jmcilhinney

If I'm understanding your question correctly:

如果我正确理解你的问题:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE '%' + @empfname + '%'", connection)

command.Parameters.AddWithValue("@empfname", input)

or:

或者:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE @empfname", connection)

command.Parameters.AddWithValue("@empfname", "%" & input & "%")

You have to concatenate the wildcards with the input text, either in your SQL code or your VB code.

您必须在 SQL 代码或 VB 代码中将通配符与输入文本连接起来。

回答by Rick James

This is the correct syntax: LIKE '%blah%', but this code does not put the quotes outside the %s: LIKE '%''" + input + "''%'".

这是正确的语法: LIKE '%blah%',但此代码不会将引号放在 %s: 之外 LIKE '%''" + input + "''%'"