在 vb.net 中运行选择查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15183124/
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
Run select query in vb.net
提问by Shri
i want to use the select query to retrieve user password from the database amsdb.mbd, the table name "user"
我想使用选择查询从数据库 amsdb.mbd 中检索用户密码,表名“user”
here is the connection string
这是连接字符串
conaccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\shirish\Desktop\AMS\New Folder\ams\ams\amsdb.mdb"
conaccess.Open()
here is the query I used:
这是我使用的查询:
Dim selcmd As New OleDbCommand("SELECT pwd FROM user WHERE uid = '" & ComboBox1.SelectedText & "'", conaccess)
Dim pwd1 As String
pwd1 = selcmd.ExecuteNonQuery()
but this gives me an error
但这给了我一个错误
Syntax error in FROM clause.
FROM 子句中的语法错误。
any corrections or suggestions?
任何更正或建议?
回答by MayogaX
The problem is because the useris a keyword, that means do you can not to use a table with that name or can not to run a query with this word.
问题是因为user是关键字,这意味着您不能使用具有该名称的表或不能使用该词运行查询。
For see all Sql Server keyword see: Reserved Keywords (Transact-SQL)
如需查看所有 Sql Server 关键字,请参阅:保留关键字 (Transact-SQL)
If do you change the table name you can to run again the query.
如果更改表名,则可以再次运行查询。
Update:
更新:
You need get the password but the method ExecuteNonQuery can not do it. For this you need to use the OledbDataReader
您需要获取密码,但 ExecuteNonQuery 方法无法获取。为此,您需要使用OledbDataReader
Exemple:
例子:
Dim selcmd As New OleDbCommand("SELECT pwd FROM userstbl WHERE uid = '" & ComboBox1.SelectedText & "'", conaccess)
Dim reader As OleDbDataReader = selcmd.ExecuteReader()
pwd1 = reader("pwd").ToString();

