用VB.net环境连接access数据库后完成一个登录界面的操作系统。
代码如下,是之前网上的大神码的。其中有一个问题:当用户名正确密码错误时,还是可以登陆,无法显示“密码错误!请重新输入密码!” 请各位大神帮助!!!
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("用户名不能为空! 请先输入用户名后再登陆")
TextBox1.Focus()
Else
Dim cnn As New OleDb.OleDbConnection
cnn.ConnectionString = "provider=Microsoft.Jet.oledb.4.0;Data source='C:\Users\chao\Desktop\我的毕业设计\数据库1.mdb'"
cnn.Open()
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter("select * from [user]", cnn)
cnn.Close()
da.Fill(ds, "user")
If ds.Tables(0).Rows.Count <> 0 Then
Dim sql As String = "select * from [user] where username='" & Trim(TextBox1.Text) & "'"
Dim da1 As New OleDb.OleDbDataAdapter(sql, cnn)
Dim ds1 As New DataSet
da1.Fill(ds1, "user")
If ds1.Tables(0).Rows.Count = 0 Then
MsgBox("用户名错误!")
TextBox1.Text = ""
TextBox1.Focus()
Else
If TextBox2.Text = "" Then
MsgBox("请输入密码!")
TextBox2.Focus()
Else
Dim sq2 As String = "select password from user where username=" & Trim(TextBox1.Text) & " and password=" & Trim(TextBox2.Text) & ""
Dim da2 As New OleDb.OleDbDataAdapter(sql, cnn)
Dim ds2 As New DataSet
da2.Fill(ds2, "user")
If ds2.Tables(0).Rows.Count = 0 Then
MsgBox("密码错误!请重新输入密码!")
TextBox2.Text = ""
TextBox2.Focus()
Else
Me.Hide()
Form2.Show()
End If
End If
End If
End If
End If
End Sub
End Class