用VB.net环境连接access数据库后完成一个登录界面的操作系统。

baidu_34815125 2016-04-27 09:23:24
代码如下,是之前网上的大神码的。其中有一个问题:当用户名正确密码错误时,还是可以登陆,无法显示“密码错误!请重新输入密码!” 请各位大神帮助!!!
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
...全文
2197 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxxxzasfuijh 2019-12-23
  • 打赏
  • 举报
回复
大佬们破案没有,学习下
sunsatan 2017-08-06
  • 打赏
  • 举报
回复
直接在SQL语句中密码字段,不怕注入式攻击么?建议建立一个用户对象然后 new 这个对象通过属性去验证密码比较好一些吧。
  • 打赏
  • 举报
回复
开发时肯定是要调试 Dim sq2 As String = "select password from................ ..... ds2.Tables(0).Rows[0][0] 之类的代码和变量,然后提出逻辑一致性分析。 看一个人编程技术到底怎么样,并不是看他(她)代码是从哪里抄来的,而是从调试技术开始看。
  • 打赏
  • 举报
回复
引用 23 楼 yj104704031 的回复:
这个应该可以的 。。。
他的意思是,看着累,就容易有逻辑 bug。 对代码进行重构和简化代码风格,也是能提高开发效率的。
  • 打赏
  • 举报
回复
引用 8 楼 baidu_34815125 的回复:
这个不是我写的...只是在网上抄网友的.... 我的数据库不熟..想直接码代码,还望海涵
逻辑设计问题,用一张纸、一支笔就能设计和分析。把逻辑搞清楚再debug代码。
yj104704031 2017-08-03
  • 打赏
  • 举报
回复
引用 20 楼 CityBird 的回复:
我只是想说你嵌套这么多层的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话说用户名会出现重复?
这个应该可以的 。。。
bszxhw 2017-08-02
  • 打赏
  • 举报
回复
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click cn = New OleDbConnection(“"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" ……";Persist Security Info=False"") cn.Open() cm = New OleDbConnection("select count(*) from login where name ='" + 用户名.Text + "'And password ='" + 密码框.Text + "'", cn) If cm.ExecuteScalar > 0 Then 'MsgBox("密码正确,进入系统!") Else MsgBox("密码错误,请重新输入!") Me.Close() End If cn.Close() End Sub
xyxcc177109 2017-07-22
  • 打赏
  • 举报
回复
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click If TextBox1.Text = "" or TextBox2.Text="" Then MsgBox("用户名或密码不能为空!") TextBox1.Focus() Exit Sub End if 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 strSql as string="select count(password) from [user] where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'" dim cmd as New OleDb.OleDbCommand(strSql, cnn) Dim i As Integer= cmd.ExecuteScalar() If i=0 Then msgBox("用户名或密码错误,请重新输入",16) Exit Sub End if End Sub
三楼の郎 2017-07-19
  • 打赏
  • 举报
回复
我只是想说你嵌套这么多层的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话说用户名会出现重复?
skzjq 2017-07-17
  • 打赏
  • 举报
回复
我有写一个,是SQL的.你要不,
OneCoderr 2017-07-12
  • 打赏
  • 举报
回复
关于单引号的问题你多调试几下就能解决。 我这个给你另外的建议:1.建议你把用户名和密码一块查询;2.用OleDbDataReader判断HasRows。 这样你的代码就变得很简单了。
yusufqq 2017-07-08
  • 打赏
  • 举报
回复
密码控件写什么都可以进去了 ,咋弄。。。
baidu_34815125 2016-05-03
  • 打赏
  • 举报
回复
引用 15 楼 notbb 的回复:
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)

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
If getpsw( "select password from user where username=‘" & Trim(TextBox1.Text) & “’”)<>trim( TextBox2.Text ) Then
MsgBox("密码错误!请重新输入密码!")
TextBox2.Text = ""
TextBox2.Focus()
Else
Me.Hide()
Form2.Show()
End If
End If
End If
cnn.Close()
End If
End If

End Sub
Public Function getpsw(ByVal s As String) As String
Try
Public cmd As New OleDbCommand
cmd.Connection = =cnn
cmd.CommandText = s
getpsw= IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar) & ""

Catch ex As Exception
getpsw= ""
End Try
End Function
End Class



运行后出错....
轻鸿万里 2016-05-01
  • 打赏
  • 举报
回复
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) 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 If getpsw( "select password from user where username=‘" & Trim(TextBox1.Text) & “’”)<>trim( TextBox2.Text ) Then MsgBox("密码错误!请重新输入密码!") TextBox2.Text = "" TextBox2.Focus() Else Me.Hide() Form2.Show() End If End If End If cnn.Close() End If End If End Sub Public Function getpsw(ByVal s As String) As String Try Public cmd As New OleDbCommand cmd.Connection = =cnn cmd.CommandText = s getpsw= IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar) & "" Catch ex As Exception getpsw= "" End Try End Function End Class
轻鸿万里 2016-04-29
  • 打赏
  • 举报
回复
你连SQL语法都搞错了,字符的值是要带单引号括起来的。不然系统默认为列名,那样查询肯定不对了。
轻鸿万里 2016-04-29
  • 打赏
  • 举报
回复
引用 楼主 baidu_34815125 的回复:
代码如下,是之前网上的大神码的。其中有一个问题:当用户名正确密码错误时,还是可以登陆,无法显示“密码错误!请重新输入密码!” 请各位大神帮助!!! 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
验证部分改成下面这个: If getpsw( "select password from user where username=‘" & Trim(TextBox1.Text) & “’”)<>trim( TextBox2.Text ) Then MsgBox("密码错误!请重新输入密码!") TextBox2.Text = "" TextBox2.Focus() Else Me.Hide() Form2.Show() End If
baidu_34633664 2016-04-29
  • 打赏
  • 举报
回复
sql2,cnn 你的2漏了吧?
baidu_34815125 2016-04-29
  • 打赏
  • 举报
回复
引用 13 楼 notbb 的回复:
你连SQL语法都搞错了,字符的值是要带单引号括起来的。不然系统默认为列名,那样查询肯定不对了。


抱歉大神,如果直接输入的话会出现下面这种情况

好像是没有定义getpsw,请问该如何改进?是要在程序的开头加上这些语句吗?
Public Function getpsw(ByVal s As String) As String
getpsw= IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar) & ""
本人真心小白,还望大神耐心指导,谢谢!!
baidu_34815125 2016-04-29
  • 打赏
  • 举报
回复
引用 5 楼 notbb 的回复:
写个方法直接给你使用吧。 重点是红色那句 Public Function getpsw(ByVal s As String) As String Try Public cmd As New OleDbCommand cmd.Connection = =cnn cmd.CommandText = s getpsw= IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar) & "" Catch ex As Exception getpsw= "" End Try End Function
请问能在我原来的基础上进行修改吗?我真的是小白,不太懂,能不能求大神把完整一点的语句码给我,万分感谢!!
baidu_34815125 2016-04-29
  • 打赏
  • 举报
回复
引用 7 楼 baidu_34633664 的回复:
sq2语句错误,没有单引号。id 和密码怎么可能是纯数值?你觉得呢!
我修改成这样子还是不行啊(请看红色的语句,还是会出现密码错误但是无法显示提示而是直接登录的情况) 求大神支招! 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
加载更多回复(8)

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧