如何使用select查询从数据库中检索数据

时间:2011-06-15 06:28:45

标签: vb.net

我必须在两个TextBox中检索数据,但数据应属于我从第一个文本框获取的tokennum。我总共有三个TextBox和一个按钮。在名为db1的数据库中,我有一个名为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先生:感谢您的指导,我更正了但现在该怎么办?如何在对应的文本框中获取相应的数据?

2 个答案:

答案 0 :(得分:0)

您的SQL查询错误

    sql = "SELECT Table1.ser FROM Table1 where Table1.token num=a"

它应该是这样的:

    sql = "SELECT Table1.ser FROM Table1 where Table1.tokennum=" & a

虽然'Table1.token num'错误,但列名中不能有空格,我希望这只是一个拼写错误,并在第二个查询中更正了相同内容。

答案 1 :(得分:0)

"SELECT Table1.ser FROM Table1 where Table1.tokennum= " + a

这是一个开始,但你的问题不是很清楚