我正在尝试创建一个登录表单并继续收到此错误:
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
答案 0 :(得分:1)
我在这里看到两个问题。
OleDbCommand
不支持将CommandType
设置为Text
的命名参数。PASSWORD
是ms-access中的保留字。其他需要加强的事情。
Using
和OleDbCommand
。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
修改强>
由于列名不是Username
和Password
,而是sUsername
和sPassword
,您必须更改CommandText
,如下所示:
.CommandText = "Select * From Tbl_Staff Where (sUsername = ?) and (sPassword = ?)"