16,717
社区成员
发帖
与我相关
我的任务
分享
我只是想说你嵌套这么多层的if else 看着不累么
If TextBox1.Text = "" Then
MsgBox("用户名不能为空! 请先输入用户名后再登陆")
TextBox1.Focus()
exit sub
end if
……
If ds1.Tables(0).Rows.Count = 0 Then
MsgBox("用户名错误!")
TextBox1.Text = ""
TextBox1.Focus()
exit sub
end if
……
这样逻辑不是清楚的多。
而且不就是验证个用户名密码嘛,没事你老查数据库干嘛,前面第一次用select * from [user] where username=……的时候不是已经把password字段的内容返回来了,那后面还查个屁啊,直接判断ds1.tables(0).rows(0).item("password")值和textbox2的内容是不是相等不就完事了,当然判断之前最好先检查一下ds1.tables(0).rows.count的值,=0没有这个用户,=1有这个用户可以去检查密码,>1话说用户名会出现重复?


求大神支招!
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