VB,NET在编写用户登录是出错提示“在没有任何数据时进行无效的读取尝试”

[复制链接]
查看11 | 回复2 | 2021-1-27 06:21:41 | 显示全部楼层 |阅读模式
代码是:
ImportsSystem.Data
ImportsSystem.Data.SqlClient
PublicClassLogin
PrivateSubButton1_Click(senderAsObject,eAsEventArgs)HandlesButton1.Click
IfTextBox1.Text=""OrTextBox2.Text=""Then
MsgBox("用户名和密码不能为空!")
Else
DimcnAsNewSqlConnection
DimpasswordAsString
DimnameAsString
DimdaAsSqlDataReader
name=TextBox1.Text
cn.ConnectionString="server=.;uid=sa;pwd=123;database=索尼手机专卖店数据库"
cn.Open()
Ifcn.State=ConnectionState.OpenThen
DimaaAsString
aa="select*from用户表where用户名='"&name&"'and密码='"&TextBox2.Text&"'"
DimcmdAsNewSqlCommand
cmd.CommandText=aa
cmd.Connection=cn
da=cmd.ExecuteReader
da.Read()
password=da.GetString(1)
IfTextBox2.Text=passwordOrpassword=""Then
MsgBox("登录成功!欢迎使用系统!",vbExclamation,"登录成功!")
Main.Show()
Else
MsgBox("登录失败!拒绝访问系统!",vbExclamation,"登录失败!")

EndIf
EndIf
EndIf
EndSub
调试时我用正确的用户名,错误的密码时在password=da.GetString(1)提示出错““在没有任何数据时进行无效的读取尝试”。为的是验证打正确的用户名时,输入错误的密码,然后提示MsgBox("登录失败!拒绝访问系统!",vbExclamation,"登录失败!")这一行。
在线求各位回答。。。
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层


做课程设计啊,各位大人帮帮忙啊。。。
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
你密码都输错了
aa="select*from用户表where用户名='"&name&"'and密码='"&TextBox2.Text&"'"
这能有结果???
da怎么可能会有东西。没有东西你怎么弄下一条da.Read()
ifda.Read=Truethen
MsgBox("登录成功!欢迎使用系统!",vbExclamation,"登录成功!")
else
MsgBox("登录失败!拒绝访问系统!",vbExclamation,"登录失败!")
endif
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行