使用 ms access 数据库在 vb.net 中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16331041/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:27:37  来源:igfitidea点击:

selecting in vb.net with ms access database

vb.netselect

提问by Sam

I want to select some data in my table and I use this code :

我想在我的表中选择一些数据并使用以下代码:

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

But there is an error in reader = cmd.ExecuteReader()line. Anyone can help me?

但是有一个错误reader = cmd.ExecuteReader()。任何人都可以帮助我吗?

采纳答案by Lingasamy Sakthivel

Correct the mistakes. The code should be

修改错误。代码应该是

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

tuser is a variable, not the content

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