使用ms访问数据库在vb.net中进行选择

时间:2013-05-02 05:37:12

标签: vb.net select

我想在表格中选择一些数据并使用此代码:

Public Class frmLogin
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Dim t As New DataTable
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader = Nothing
    Dim tuser As String = txtUsername.Text

    Dim sql As String = "SELECT * FROM dosen WHERE nip=tuser"
    Try
        cmd = New OleDbCommand(sql, conn)
        reader = cmd.ExecuteReader()

        While reader.Read
            MessageBox.Show(reader.GetString(0).ToString & _
                            vbTab & vbTab & reader.GetString(1).ToString)
        End While
    Finally
        If reader IsNot Nothing Then reader.Close()
    End Try

End Sub

reader = cmd.ExecuteReader()行存在错误。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

纠正错误。代码应该是

Dim reader As OleDbDataReader ' no need for nothing
Dim adapter As New OleDbDataAdapter()
Dim sql As String = "SELECT * FROM dosen WHERE nip='" & tuser & "'"

tuser是一个变量,而不是内容