vb.net 在没有数据网格视图的 vb 中从文本框中搜索/查询访问数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23902945/
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
Search / Query an access database from textbox in vb wthout datagrid view
提问by Playergoodi
Hey guys I need a little assistance in doing a basic search query from my form program to connect to an access database. basically I want to learn how to with the click of my query button, search text entered in a textbox and show the corresponding data in another text box, based on a match found. Any help would be greatly appreciated.
嗨,伙计们,我需要一点帮助,从我的表单程序执行基本搜索查询以连接到访问数据库。基本上我想学习如何通过单击我的查询按钮,搜索在文本框中输入的文本并根据找到的匹配在另一个文本框中显示相应的数据。任何帮助将不胜感激。
here is the code of my query button so far..
这是到目前为止我的查询按钮的代码..
Private Sub btnQuery_Click(sender As Object, e As EventArgs) Handles btnQuery.Click
con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Eric\Documents\Fadv.accdb")
con.Open()
Dim id As Integer
Dim SQL As String = "SELECT FirstName, NAME FROM info WHERE FirstName=" & txtFirstName.Text & ""
Using cmd As New OleDbCommand(SQL, con)
id = Val(InputBox("Enter Query Criteria"))
MsgBox(SQL)
cmd.ExecuteNonQuery()
con.Close()
End Using
结束使用
Im having a little trouble trying to figure out the (connectionstring) Im not sure if this is something I have to declare so far here is that my code, any guidance would be greatly appreciated.
我在试图找出(连接字符串)时遇到了一些麻烦,我不确定这是否是我到目前为止必须声明的内容,我的代码,任何指导将不胜感激。
` Private Sub btnQuery_Click(sender As Object, e As EventArgs) Handles btnQuery.Click
` Private Sub btnQuery_Click(sender As Object, e As EventArgs) 处理 btnQuery.Click
Using con As New OleDbCommand(connectionstring)
Dim cmd As New OleDbCommand(SQL, con)
con.open()
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While reader.Read()
txtFirstName.Text = reader(0).ToString())
End While
reader.Close()
End Using
`
`
回答by Mark C.
ExecuteNonQuery()is supposed to be used to write to the database, not read from it. "Although the ExecuteNonQuery returns no rows...[]" says the MSDN Documentation.
ExecuteNonQuery()应该用于写入数据库,而不是从中读取。“尽管 ExecuteNonQuery 不返回任何行...[]”说MSDN 文档。
There's a couple ways to perform what you're trying to do. I suggest looking at the example herebecause it will show you how to perform a query using OleDb.
有几种方法可以执行您正在尝试执行的操作。我建议查看这里的示例,因为它将向您展示如何使用OleDb.
To get the data from your query to a textbox, you can do something like:
要将查询中的数据获取到文本框,您可以执行以下操作:
Using con As New OleDbConnection(connectionstring) //Your connection string
Dim command As New OleDbCommand(SQL, con)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
TextBox1.Text = reader(0).ToString())
End While
reader.Close()
End Using
This linkprovides even more clarification, and will greatly help you if you read what it has to say. Specifically:
此链接提供了更多说明,如果您阅读其中的内容,将对您有很大帮助。具体来说:
"You use the Read method of the DataReader object to obtain a row from the results of the query. You can access each column of the returned row by passing the name or ordinal reference of the column to the DataReader. However, for best performance, the DataReader provides a series of methods that allow you to access column values in their native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on)."
"You use the Read method of the DataReader object to obtain a row from the results of the query. You can access each column of the returned row by passing the name or ordinal reference of the column to the DataReader. However, for best performance, the DataReader provides a series of methods that allow you to access column values in their native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on)."
Also note: you want to paramaterize your queries.
另请注意:您希望参数化您的查询。

