一个数据查询问题,请高手帮忙·!~

chinanetspy 2003-04-02 01:13:04
我想做一个用户登陆窗口测试,首先判断用户是否存在,如果存在则提示用户存在,如果无则提示无此用户
可是查到数据库中的数据如果有就很正常,没有的时候就报错,请高手指点。
关键代码如下:
Dim b As Variant
b = Text1.Text
Set rs = New ADODB.Recordset
rs.Open "select * from login where user=""" & b & """", conn, adOpenDynamic, adLockOptimistic
If Text1.Text = rs!user Then
Text2.Text = "该用户存在"
Unload Me
Else
Text2.Text = "无此用户"


End If
...全文
52 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
s6w6k 2003-04-02
  • 打赏
  • 举报
回复
dim rs As ADODB.Recordset
Set rsuser = New ADODB.Recordset
rsuser.Open "select * from login where user=""" & b & """" , conn, adOpenStatic, adLockOptimistic

If rsuser.RecordCount > 0 Then
Text2.Text = "该用户存在"
Unload Me
Else
Text2.Text = "无此用户"
Exit sub
Endif
980 2003-04-02
  • 打赏
  • 举报
回复
Dim b As Variant
b = Text1.Text
Set rs = New ADODB.Recordset
rs.Open "select * from login where user=""" & b & """", conn, adOpenDynamic, adLockOptimistic
if rs.recordcount>=1 then
Text1.Text = rs!user
Text2.Text = "该用户存在"

else
text2 = "无此用户"
exit sub


End If
sxmines 2003-04-02
  • 打赏
  • 举报
回复
其实道理很简单就是判断你使用sql语句得到的Recordset的记录集中是否有记录
即当
rs.recordcount>0
表示,数据库中有该记录,继而还可以判断记录的个数
当然使用
rs.eof and rs.bof
也可以判断rs是否为空

此外需要注意的是,你在处理用户名和密码是需要对此进行处理
即需要将其中的'号处理,
使用
Replace(text1.Text, "'", "''")函数将一个引号变成两个引号
这样做是为了避免sql语句的漏洞
youxia001 2003-04-02
  • 打赏
  • 举报
回复
Dim b As Variant
b = trim(Text1.Text)
if instr(1,b,"'")>0 then
msgbox "不能含有 “ '“ 此符号!"
exit sub
end if
Set rs = New ADODB.Recordset
rs.Open "select * from login where user='" & b & "'", conn, adOpenDynamic, adLockOptimistic
If not rs.eof Then
Text2.Text = "该用户存在"
Unload Me
Else
Text2.Text = "无此用户"
End If
cbr7619 2003-04-02
  • 打赏
  • 举报
回复
不好意思
If rs.RecordCount=0 then
Text2.Text = "无此用户"
Exit Sub
Else
Text2.Text = "该用户存在"
Endif

还有user=""" & b & """" ==>user="'" & b & "'"
dim b as string
marcosong 2003-04-02
  • 打赏
  • 举报
回复
If rs.EOF then
Text2.Text = "无此用户"
Exit Sub
Else
Text2.Text = "该用户存在"
Endif
marcosong 2003-04-02
  • 打赏
  • 举报
回复
用rs.eof来判断是否有数据就可以了
chinanetspy 2003-04-02
  • 打赏
  • 举报
回复
提示编译错误:未找到方法或数据成员
cbr7619 2003-04-02
  • 打赏
  • 举报
回复
没有的时候 rs =空
If rs.Count=0 then
Text2.Text = "无此用户"
Exit Sub
Else
Text2.Text = "该用户存在"
Endif

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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