vb.net SQL Like 语句在 Visual Basic 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13583209/
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 11:19:55  来源:igfitidea点击:

SQL Like statement not working in Visual Basic

sqlvb.netdatasetsql-like

提问by JH95

Dim strText As String = tbRefine.Text
Dim sql As String = "SELECT user_name,forename,surname,game_cash,reg_group FROM tblGame WHERE user_name LIKE '" + strSearchText + "' & '*'"
Dim dsRefine As New DataSet
GetDataset(sql, "tblGame", dsRefine)

MsgBox(dsRefine.Tables("tblGame").Rows(0).Item(2).ToString)

This is not working, it crashes and says there is nothing in the dataset. I know the dataset function works as its worked successfully before. When i print out the sql statement into microsoft access it works fine. What am i doing wrong

这不起作用,它崩溃并说数据集中没有任何内容。我知道数据集函数的工作原理是以前成功的。当我将 sql 语句打印到 microsoft access 时,它工作正常。我究竟做错了什么

回答by juergen d

Try this:

尝试这个:

"SELECT user_name,forename,surname,game_cash,reg_group
 FROM tblGame
 WHERE user_name LIKE '%" + strSearchText + "%'"

回答by ayman saboura

Try to use the RTRIM()function in your line:

尝试RTRIM()在您的行中使用该功能:

Dim sql As String = "SELECT user_name,forename,surname,game_cash,reg_group FROM tblGame WHERE RTRIM(user_name) LIKE '" + strSearchText + "' & '*'"

Dim sql As String = "SELECT user_name,forename,surname,game_cash,reg_group FROM tblGame WHERE RTRIM(user_name) LIKE '" + strSearchText + "' & '*'"

回答by Edward

I think there's one more thing to be mentioned: the "*" wildcard character works for the "Like" operator in VB/VBA/MS-Access, but not in T-SQL. The correct wildcard character for the "Like" operator in T-SQL is "%". That's why this T-SQL statement:

我认为还有一件事要提及:“*”通配符适用于 VB/VBA/MS-Access 中的“Like”运算符,但不适用于 T-SQL。T-SQL 中“Like”运算符的正确通配符是“%”。这就是为什么这个 T-SQL 语句:

Select... WHERE ... LIKE 'sText*'

returned no data without any syntax error in MS-SQL(using T-SQL), but works in MS-Access.

在 MS-SQL(使用 T-SQL)中没有任何语法错误的情况下没有返回任何数据,但在 MS-Access 中有效。

回答by Simon

What about leading or trailing % symbols in your like?

你喜欢的前导或尾随 % 符号怎么样?

At the moment you will end up with a where clause like:

目前,您将得到一个 where 子句,例如:

LIKE 'searchtext''*'

which looks a bit odd (I assume SQL server?).

这看起来有点奇怪(我假设 SQL 服务器?)。

回答by David Lloyd Brookes

It's wiser to use SQL parameters as your method is open to SQL injection. The link below will help with how to format the SQL statement. I would also suggest doing it via a store procedure, but hats optional...

使用 SQL 参数更明智,因为您的方法对 SQL 注入开放。下面的链接将帮助您了解如何格式化 SQL 语句。我还建议通过存储过程来做,但帽子是可选的......

http://forums.asp.net/t/1256985.aspx

http://forums.asp.net/t/1256985.aspx