当我使用Access中的用户名和密码运行程序时,它仍然显示:
输入有效的用户名和密码
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Then
MsgBox("Enter Username and password", MsgBoxStyle.Exclamation
)
Else
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
Dim cmd As New OleDbCommand("select count(*)from login where Username and Password=?", connection)
cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = TextBox1.Text
cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = TextBox2.Text
Dim count = Convert.ToInt32(cmd.ExecuteScalar())
cmd.Dispose()
connection.Close()
If (count > 0) Then
MsgBox("Login Sucessful", MsgBoxStyle.Information)
Else
MsgBox("please enter valid username and password", MsgBoxStyle.Critical)
End If
End If
End Sub
End Class
答案 0 :(得分:2)
尝试使用有效的语法:
"select count(*) from login where Username=? and Password=?"
答案 1 :(得分:0)
这是您的代码吗?连接数据库是否有问题?
Dim cmd As New OleDbCommand("select count(*)from login where Username=@1 and Password=@2", connection)
cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = TextBox1.Text
cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = TextBox2.Text