我只需要一点帮助,我已经有一个登录,但我只是想创建一个提示,如果我输入错误的用户名和正确的密码,对话框显示只有用户名不正确,如果老虎钳反之,对话框显示密码不正确。如果用户名和密码都不正确,对话框显示用户名和密码不正确。它在MS ACCESS中连接,这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connection As New OleDbConnection
connection = New OleDb.OleDbConnection
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"
Dim cmd As OleDbCommand = New OleDbCommand("SELECT User,Password FROM tblUser where User=? and Password=?", connection)
cmd.Parameters.AddWithValue("User", Me.TextBox1.Text)
cmd.Parameters.AddWithValue("Password", Me.TextBox2.Text)
Try
connection.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
read.Read()
If Me.TextBox1.Text.ToLower() = read.Item("user").ToString And Me.TextBox2.Text.ToLower = read.Item("Password").ToString Then
MsgBox("Login successful")
Me.Hide()
Form2.Show()
End If
Else
MsgBox("Login unsuccessful,no connection", MsgBoxStyle.OkOnly, "Invalid")
End If
read.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Close()
End Try
End Sub
答案 0 :(得分:1)
试试这个:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connection As New OleDbConnection
connection = New OleDb.OleDbConnection
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"
Dim cmd As OleDbCommand = New OleDbCommand("SELECT User,Password FROM tblUser where User=?", connection)
cmd.Parameters.AddWithValue("User", Me.TextBox1.Text)
Try
connection.Open()
Dim read As OleDbDataReader = cmd.ExecuteReader()
If read.HasRows Then
read.Read()
If Me.TextBox2.Text.ToLower = read.Item("Password").ToString Then
MsgBox("Login successful")
Me.Hide()
Form2.Show()
Else
MsgBox("Login unsuccessful, Incorrect Password", MsgBoxStyle.OkOnly, "Invalid")
End If
Else
MsgBox("Login unsuccessful, Invalid UserName", MsgBoxStyle.OkOnly, "Invalid")
End If
read.Close()
Catch ex As OleDbException
MsgBox("Login unsuccessful,no connection", MsgBoxStyle.OkOnly, "Invalid")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Close()
End Try
End Sub