vb.net VB 2008 使用 MS ACCESS 登录用户名和密码

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

VB 2008 Log in Username and password withg MS ACCESS

vb.netms-access

提问by EysAce

i just need a little help here, I already have a log in, but i just want to create a prompt that if I input a incorrect username and correct password, the dialog box says that the username only is incorrect and if vise versa, dialog box says that the password is incorrect. and if both username and password is incorrect, dialog box says username and password is incorrect. It is connected in MS ACCESS, here is my code:

我只是需要一点帮助,我已经登录了,但我只想创建一个提示,如果我输入了错误的用户名和正确的密码,对话框会说用户名不正确,反之亦然,对话框框说密码不正确。如果用户名和密码都不正确,对话框会说用户名和密码不正确。它在 MS ACCESS 中连接,这是我的代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connection As New OleDbConnection

    connection = New OleDb.OleDbConnection
    connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT User,Password FROM tblUser where User=? and Password=?", connection)

    cmd.Parameters.AddWithValue("User", Me.TextBox1.Text)
    cmd.Parameters.AddWithValue("Password", Me.TextBox2.Text)

    Try
        connection.Open()
        Dim read As OleDbDataReader = cmd.ExecuteReader()

        If read.HasRows Then
            read.Read()

            If Me.TextBox1.Text.ToLower() = read.Item("user").ToString And Me.TextBox2.Text.ToLower = read.Item("Password").ToString Then
                MsgBox("Login successful")
                Me.Hide()
                Form2.Show()

            End If

        Else
            MsgBox("Login unsuccessful,no connection", MsgBoxStyle.OkOnly, "Invalid")
        End If

        read.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        connection.Close()
    End Try

End Sub

回答by Pradeep Kumar

Try this:

尝试这个:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connection As New OleDbConnection

    connection = New OleDb.OleDbConnection
    connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT User,Password FROM tblUser where User=?", connection)

    cmd.Parameters.AddWithValue("User", Me.TextBox1.Text)

    Try
        connection.Open()
        Dim read As OleDbDataReader = cmd.ExecuteReader()

        If read.HasRows Then
            read.Read()

            If Me.TextBox2.Text.ToLower = read.Item("Password").ToString Then
                MsgBox("Login successful")
                Me.Hide()
                Form2.Show()
            Else
                MsgBox("Login unsuccessful, Incorrect Password", MsgBoxStyle.OkOnly, "Invalid")
            End If

        Else
            MsgBox("Login unsuccessful, Invalid UserName", MsgBoxStyle.OkOnly, "Invalid")
        End If
        read.Close()
    Catch ex As OleDbException
        MsgBox("Login unsuccessful,no connection", MsgBoxStyle.OkOnly, "Invalid")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        connection.Close()
    End Try

End Sub