vb.net 如何使用选择查询从数据库中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6353889/
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
how to retrieve data from database using select query
提问by teena gupta
I have to retrieve data in two TextBoxes but the data should belong to tokennum
that I am getting from first text box. I have a total of three TextBoxes and one button. In a database called db1
I have a table named Table1
and two fields ser
as serial number, tokennum
for token number and name
for name of employees.
我必须在两个tokennum
文本框中检索数据,但数据应该属于我从第一个文本框中获取的数据。我总共有三个文本框和一个按钮。在名为db1
I的数据库中,有一个名为的表Table1
和两个字段ser
作为序列号,tokennum
用于令牌号和name
员工姓名。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim a As Integer
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Documents and Settings\trainee-ng-it\Desktop\Subhedar Sir\New Folder (2)\db1.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
a = Val(TextBox1.Text)
sql = "SELECT Table1.ser FROM Table1 where Table1.token num=a"
da = New OleDb.OleDbDataAdapter(sql, con)
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "db1")
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
End Sub
End Class
@dhruva sir:thanks for guidance,i corrected that but now how to proceed?how to get respective data in the correspoding textboxes?
@dhruva 先生:感谢您的指导,我更正了,但现在如何进行?如何在相应的文本框中获取相应的数据?
回答by Dhruva Sagar
Your sql query is wrong
你的sql查询错误
sql = "SELECT Table1.ser FROM Table1 where Table1.token num=a"
It should be something like this :
它应该是这样的:
sql = "SELECT Table1.ser FROM Table1 where Table1.tokennum=" & a
Although the 'Table1.token num' is wrong, there cannot be a space within a column name, I hope that was just a typo and have corrected the same in the second query.
虽然 'Table1.token num' 是错误的,列名中不能有空格,我希望这只是一个错字,并在第二个查询中更正了同样的问题。
回答by pistacchio
"SELECT Table1.ser FROM Table1 where Table1.tokennum= " + a
This is a start, but your question is not very clear
这是一个开始,但你的问题不是很清楚