Visual Basic System.Data.OleDb.OleDbException:'没有给出一个或多个必需参数的值。'

时间:2018-05-28 10:57:00

标签: vb.net ms-access

我正在尝试创建一个登录表单并继续收到此错误:

  

Visual Basic System.Data.OleDb.OleDbException:'没有给出一个值   或更多必需参数。'

我正在使用带vb的访问数据库,所有内容拼写正确,但我一直收到此错误?

Private Sub Btn_Login_Click(sender As Object, e As EventArgs) Handles Btn_Login.Click
    If DbConnect() Then
        Dim SQLCmd As New OleDbCommand
        With SQLCmd
            .Connection = cn
            .CommandText = "Select * From Tbl_Staff Where (Username = @sUsername) and (Password = @sPassword)"
            .Parameters.AddWithValue("@sUsername", Txt_Username.Text)
            .Parameters.AddWithValue("@sPassword", Txt_Password.Text)

            Dim rs As OleDbDataReader = .ExecuteReader()

            If rs.Read Then
                PublicFirstname = rs("Firstname")
                PublicStaffID = rs("StaffID")
                'PublicAccessLevel = rs("AccessLevel")
                frmMainMenu.Show()
            Else
                MsgBox("Sorry, incorrect log in details entered")
            End If
            rs.Close()

修改
更改了我的代码,但仍然收到同样的错误:

If DbConnect() Then
    Using SQLCmd As New OleDbCommand
        With SQLCmd
            .Connection = cn
            .CommandText = "Select * From Tbl_Staff Where (Username = ?) and ([Password] = ?)"
            .Parameters.Add("@sUsername", OleDbType.VarChar).Value = Txt_Username.Text
            .Parameters.Add("@sPassword", OleDbType.VarChar).Value = Txt_Password.Text

            Using rs As OleDbDataReader = .ExecuteReader()
                If rs.Read Then
                Else
                    MsgBox("Sorry, incorrect log in details entered")
                End If
                rs.Close()
            End Using
        End With
    End Using
End If

1 个答案:

答案 0 :(得分:1)

我在这里看到两个问题。

  1. OleDbCommand不支持将CommandType设置为Text的命名参数。
    提供的订单非常丰富。
  2. PASSWORD是ms-access中的保留字。
    将其放在方括号中。
  3. 其他需要加强的事情。

    • UsingOleDbCommand
    • 使用OleDbDataReader
    • 不要以明文形式存储密码。

    所以你的代码应如下所示:

    Using SQLCmd As New OleDbCommand
        With SQLCmd
            .Connection = cn
            .CommandText = "Select * From Tbl_Staff Where (Username = ?) and ([Password] = ?)"
            .Parameters.Add("@sUsername", OleDbType.VarChar).Value = Txt_Username.Text
            .Parameters.Add("@sPassword", OleDbType.VarChar).Value = Txt_Password.Text
    
            Using rs As OleDbDataReader = .ExecuteReader()
                If rs.Read Then
                Else
                    MsgBox("Sorry, incorrect log in details entered")
                End If
                rs.Close()
            End Using
        End With
    End Using
    

    修改
    由于列名不是UsernamePassword,而是sUsernamesPassword,您必须更改CommandText,如下所示:

    .CommandText = "Select * From Tbl_Staff Where (sUsername = ?) and (sPassword = ?)"