首页 > 解决方案 > 连接尝试显示“输入有效的用户名和密码”

问题描述

当我使用 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

标签: vb.netvisual-studioms-access-2010

解决方案


尝试使用有效的语法:

"select count(*) from login where Username=? and Password=?"

推荐阅读