SQL Like statement not working in Visual Basic

强颜欢笑 提交于 2019-12-13 09:49:28

问题


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


回答1:


Try this:

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



回答2:


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

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




回答3:


What about leading or trailing % symbols in your like?

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

LIKE 'searchtext''*'

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




回答4:


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...

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




回答5:


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:

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

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



来源:https://stackoverflow.com/questions/13583209/sql-like-statement-not-working-in-visual-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!