Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MS Access//两种用户类型登录VB.net函数_Vb.net_Ms Access - Fatal编程技术网

使用MS Access//两种用户类型登录VB.net函数

使用MS Access//两种用户类型登录VB.net函数,vb.net,ms-access,Vb.net,Ms Access,您好,我正在尝试创建一个登录系统,其中将有一个管理员和超级管理员登录,我只是按照网上的一些教程,并尝试在我自己,我是新的vb,我只是想尝试一下。但是,执行以下代码时,它既不是登录也不是登录。它总是去catch部分,它说它没有连接到数据库。这里是代码错误btw System.Data.dll中发生了类型为“System.Data.OleDb.OledBeException”的第一次意外异常 我将第一个If语句中的或更改为OrElse,以便在第一个条件为真时检查第二个条件,从而使代码短路 连接是珍贵

您好,我正在尝试创建一个登录系统,其中将有一个管理员和超级管理员登录,我只是按照网上的一些教程,并尝试在我自己,我是新的vb,我只是想尝试一下。但是,执行以下代码时,它既不是登录也不是登录。它总是去catch部分,它说它没有连接到数据库。这里是代码错误btw

System.Data.dll中发生了类型为“System.Data.OleDb.OledBeException”的第一次意外异常


我将第一个
If
语句中的
更改为
OrElse
,以便在第一个条件为真时检查第二个条件,从而使代码短路

连接是珍贵的对象,需要关闭和处理。尽可能晚开,尽可能快关<代码>使用…结束使用
块确保这一点,即使出现错误。在这种情况下,使用
命令对象也包括在

您可以将连接字符串直接传递给连接的构造函数。同样,将命令文本和连接传递给命令的构造函数

我将
Select
语句更改为仅检索
UserType
,因为这就是方法中使用的全部内容。不要从数据库中提取超出必要范围的信息

Private Sub OpCode()
    If userBox.Text = "" OrElse passwordBox.Text = "" Then
        MessageBox.Show("Username and/or password are blank", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim AdminType As String
    Using conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ResortReservationSystem.accdb"),
            sqlComm As New OleDbCommand("SELECT UserType FROM tbl_user WHERE username= @User AND [password] = @Password", conn)
        With sqlComm.Parameters
            .Add("@User", OleDbType.VarChar, 100).Value = userBox.Text
            .Add("@Password", OleDbType.VarChar, 100).Value = passwordBox.Text
        End With
        conn.Open()
        AdminType = sqlComm.ExecuteScalar.ToString
    End Using
    If String.IsNullOrEmpty(AdminType) Then
        MessageBox.Show("Username and Password do not match.", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        userBox.Text = ""
        passwordBox.Text = ""
        userBox.Focus()
    ElseIf AdminType = "Admin" Then
        MenuForm.Show()
        Me.Hide()
    ElseIf AdminType = "SuperAdmin" Then
        EmployeeForm.Show()
        Me.Hide()
    End If
End Sub
学习使用
参数
。它使sql语句更易于编写,加快了查询速度并防止sql注入

由于我们只检索单个数据段,因此可以使用
.ExecuteScalar
获取结果集第一行的第一列

关闭连接和命令并使用
结束
后,我们可以处理从数据库检索到的数据

Private Sub OpCode()
    If userBox.Text = "" OrElse passwordBox.Text = "" Then
        MessageBox.Show("Username and/or password are blank", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim AdminType As String
    Using conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ResortReservationSystem.accdb"),
            sqlComm As New OleDbCommand("SELECT UserType FROM tbl_user WHERE username= @User AND [password] = @Password", conn)
        With sqlComm.Parameters
            .Add("@User", OleDbType.VarChar, 100).Value = userBox.Text
            .Add("@Password", OleDbType.VarChar, 100).Value = passwordBox.Text
        End With
        conn.Open()
        AdminType = sqlComm.ExecuteScalar.ToString
    End Using
    If String.IsNullOrEmpty(AdminType) Then
        MessageBox.Show("Username and Password do not match.", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        userBox.Text = ""
        passwordBox.Text = ""
        userBox.Focus()
    ElseIf AdminType = "Admin" Then
        MenuForm.Show()
        Me.Hide()
    ElseIf AdminType = "SuperAdmin" Then
        EmployeeForm.Show()
        Me.Hide()
    End If
End Sub

我将第一个
If
语句中的
更改为
OrElse
,以便在第一个条件为真时检查第二个条件,从而使代码短路

连接是珍贵的对象,需要关闭和处理。尽可能晚开,尽可能快关<代码>使用…结束使用
块确保这一点,即使出现错误。在这种情况下,使用
命令对象也包括在

您可以将连接字符串直接传递给连接的构造函数。同样,将命令文本和连接传递给命令的构造函数

我将
Select
语句更改为仅检索
UserType
,因为这就是方法中使用的全部内容。不要从数据库中提取超出必要范围的信息

Private Sub OpCode()
    If userBox.Text = "" OrElse passwordBox.Text = "" Then
        MessageBox.Show("Username and/or password are blank", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim AdminType As String
    Using conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ResortReservationSystem.accdb"),
            sqlComm As New OleDbCommand("SELECT UserType FROM tbl_user WHERE username= @User AND [password] = @Password", conn)
        With sqlComm.Parameters
            .Add("@User", OleDbType.VarChar, 100).Value = userBox.Text
            .Add("@Password", OleDbType.VarChar, 100).Value = passwordBox.Text
        End With
        conn.Open()
        AdminType = sqlComm.ExecuteScalar.ToString
    End Using
    If String.IsNullOrEmpty(AdminType) Then
        MessageBox.Show("Username and Password do not match.", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        userBox.Text = ""
        passwordBox.Text = ""
        userBox.Focus()
    ElseIf AdminType = "Admin" Then
        MenuForm.Show()
        Me.Hide()
    ElseIf AdminType = "SuperAdmin" Then
        EmployeeForm.Show()
        Me.Hide()
    End If
End Sub
学习使用
参数
。它使sql语句更易于编写,加快了查询速度并防止sql注入

由于我们只检索单个数据段,因此可以使用
.ExecuteScalar
获取结果集第一行的第一列

关闭连接和命令并使用
结束
后,我们可以处理从数据库检索到的数据

Private Sub OpCode()
    If userBox.Text = "" OrElse passwordBox.Text = "" Then
        MessageBox.Show("Username and/or password are blank", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim AdminType As String
    Using conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ResortReservationSystem.accdb"),
            sqlComm As New OleDbCommand("SELECT UserType FROM tbl_user WHERE username= @User AND [password] = @Password", conn)
        With sqlComm.Parameters
            .Add("@User", OleDbType.VarChar, 100).Value = userBox.Text
            .Add("@Password", OleDbType.VarChar, 100).Value = passwordBox.Text
        End With
        conn.Open()
        AdminType = sqlComm.ExecuteScalar.ToString
    End Using
    If String.IsNullOrEmpty(AdminType) Then
        MessageBox.Show("Username and Password do not match.", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        userBox.Text = ""
        passwordBox.Text = ""
        userBox.Focus()
    ElseIf AdminType = "Admin" Then
        MenuForm.Show()
        Me.Hide()
    ElseIf AdminType = "SuperAdmin" Then
        EmployeeForm.Show()
        Me.Hide()
    End If
End Sub

你能调试你的代码吗?代码中的哪一行引发异常?异常消息是什么?您需要在定义它之后和访问项目之前调用
sqlRead.Read()
。最好在If语句中执行此操作。如果If成功,那么您知道用户名/密码组合很好;如果失败,则说明用户输入错误。顺便说一句,还有一些其他问题需要解决:用纯文本存储密码被认为是不好的做法;尽管您使用的是MS Access,这在一定程度上缓解了SQL注入的影响,但您仍然容易受到攻击。我意识到你只是在试验,但在某个时候你也应该研究散列技术和参数的使用。@ChetanRanpariya如果sqlRead.Item(“userType”)=“Admin”,我会在这一行出错然后它显示>System.Data.dll中发生了类型为“System.InvalidOperationException”的未处理异常附加信息:行/列不存在数据。但我有这方面的数据你读过我的评论吗?出现该错误的原因是您没有调用sqlRead.Read()。在尝试访问数据之前必须调用此函数。是否能够调试代码?代码中的哪一行引发异常?异常消息是什么?您需要在定义它之后和访问项目之前调用
sqlRead.Read()
。最好在If语句中执行此操作。如果If成功,那么您知道用户名/密码组合很好;如果失败,则说明用户输入错误。顺便说一句,还有一些其他问题需要解决:用纯文本存储密码被认为是不好的做法;尽管您使用的是MS Access,这在一定程度上缓解了SQL注入的影响,但您仍然容易受到攻击。我意识到你只是在试验,但在某个时候你也应该研究散列技术和参数的使用。@ChetanRanpariya如果sqlRead.Item(“userType”)=“Admin”,我会在这一行出错然后它显示>System.Data.dll中发生了类型为“System.InvalidOperationException”的未处理异常附加信息:行/列不存在数据。但我有这方面的数据你读过我的评论吗?出现该错误的原因是您没有调用sqlRead.Read()。在尝试访问数据之前,必须调用此函数。