vb.net和MS Access 2016登录表单

时间:2019-02-23 07:03:27

标签: vb.net

我创建了一个登录表单,当我输入“用户名”和“密码”时,当我单击“登录”按钮时,它引发了一个错误“对象引用未设置为对象的实例” 这是代码。

Imports System.Data.OleDb
Imports System.Data

Public Class Form1
    Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
        If Len(Trim(txtusername.Text)) = 0 Then
            MessageBox.Show("Please Enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtusername.Focus()
            Exit Sub
        End If

        If Len(Trim(txtpassword.Text)) = 0 Then
            MessageBox.Show("Please Enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtpassword.Focus()
            Exit Sub
        End If

        Try
            Dim myConnection As New OleDbConnection
            myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\Users\Azam\Desktop\Monitoring DB\MonitoringDB.accdb")
            Dim myCommand As New OleDbCommand
            myCommand = New OleDbCommand("SELECT UserName, Password FROM Users WHERE UserName=@UserName AND Password=@Password")
            Dim uName As New OleDbParameter("@UserName", SqlDbType.VarChar)
            Dim uPassword As New OleDbParameter("@Password", SqlDbType.VarChar)
            uName.Value = txtusername.Text
            uPassword.Value = txtpassword.Text
            myCommand.Parameters.Add(uName)
            myCommand.Parameters.Add(uPassword)
            myCommand.Connection.Open()

            Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
            Dim Login As Object = 0
            If myReader.HasRows Then
                myReader.Read()
                Login = myReader(Login)

            End If
            If Login = Nothing Then
                MsgBox("Login failed. Please Try Again", MsgBoxStyle.Critical, "Login Denied")
                txtusername.Clear()
                txtpassword.Clear()
                txtusername.Focus()
            Else
                ProgressBar1.Visible = True
                ProgressBar1.Maximum = 5000
                ProgressBar1.Minimum = 0
                ProgressBar1.Value = 4
                ProgressBar1.Step = 1
                For i = 0 To 5000
                    ProgressBar1.PerformStep()
                Next
                FrmMain.ToolStripStatusLabel2.Text = txtusername.Text
                Me.Hide()
                FrmMain.Show()

            End If
            myCommand.Dispose()
            myConnection.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

看看是否可以解决此问题。

Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
    If (Trim(txtusername.Text)).Length = 0 Then 'Don't use Len It is from vb6 and just around for old code
        MessageBox.Show("Please Enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        'Don't use Focus see docs for why
        txtusername.Select
        Exit Sub
    End If

    If (Trim(txtpassword.Text)).Length = 0 Then
        MessageBox.Show("Please Enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtpassword.Select
        Exit Sub
    End If
    Dim Login As Integer
    Try
        'Whenever you use New you create a new object - don't do it twice
        Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\Users\Azam\Desktop\Monitoring DB\MonitoringDB.accdb")
            'pass both the SQL statement and the to the constructor of the command
            Using myCommand As New OleDbCommand("SELECT Count(*) FROM Users WHERE UserName=@UserName AND Password=@Password", myConnection)
                'You can't use SqlDbType with an OledbCommand
                myCommand.Parameters.Add("@UserName", OleDbType.VarChar).Value = txtusername.Text
                myCommand.Parameters.Add("@Password", OleDbType.VarChar).Value = txtpassword.Text
                myConnection.Open()
                'All we need is the count, not the data
                Login = CInt(myCommand.ExecuteScalar)
            End Using 'The Using blocks ensure that your database objects are closed and disposed
        End Using
        If Login = 0 Then
            MsgBox("Login failed. Please Try Again", MsgBoxStyle.Critical, "Login Denied")
            txtusername.Clear()
            txtpassword.Clear()
            txtusername.Select
        Else
            ProgressBar1.Visible = True
            ProgressBar1.Maximum = 5000
            ProgressBar1.Minimum = 0
            ProgressBar1.Value = 4
            ProgressBar1.Step = 1
            For i = 0 To 5000
                ProgressBar1.PerformStep()
            Next
            FrmMain.ToolStripStatusLabel2.Text = txtusername.Text
            Me.Hide()
            FrmMain.Show()
        End If
        myCommand.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

当然,在真实的应用程序中,您永远不会将密码存储为纯文本。