怪,如果连接字符串设错了,照样能显示登录成功,为何,见代码:

wyzg_web 2005-05-09 08:17:50
怪,如果连接字符串设错了,照样能显示登录成功,为何,见代码:
我试了,如果在连接函数中有on error resume next,则连接串错时,打开记录集还有记录,真怪了。
当然如果去了on error resume next,则会在浏览器中报错。我就是不明白,就是忽略错误,也无法取到记录集呀?我是用rs.recordcount判断的。现把用到的两个asp文件贴出来,大家指教一下。
我的连接函数如下:
文件名为:generaltools.asp
<!-- VB Script Document -->
<%
dim conn,weburl,strconn
'weburl="http://" & request.servervariables("server_name") & "/"
on error resume next
strconn = "driver={sql server};"
strconn = strconn & "server=(local);"
strconn = strconn & "database=fjfj;"
strconn = strconn & "uid=sa;"
strconn = strconn & "pwd=0000"
sub openconn()
set conn = server.createobject("adodb.connection")
conn.open strconn
end sub

sub closeconn()
if isobject(conn) then
conn.close
set conn = nothing
end if
end sub
%>
----------------------------------------------------
我的验证表单文件:
<!-- #include file = "generaltools.asp"-->
<html>

<head>

<title>登陆</title>
</head>

<body>


<div align center>
<%
dim strusername
dim strpassword
dim rs
dim sql
'response.write "ok" & "<br>"
strusername = request.form("txtusername") '用户名
strpassword = request.form("txtpassword") '密码
if strusername = "" then
'response.redirect "ghindex.htm"
end if
if strpassword = "" then
'response.redirect "ghindex.htm"
end if
if len(strusername) > 10 then
response.write "用户名太长"
'response.redirect "ghindex.htm"
end if
if len(strpassword) > 6 then
response.write "密码太长"
'response.redirect "ghindex.htm"
end if




'连接串:
call openconn '调连接函数

set rs=server.createobject("adodb.recordset")
sql = "select 编号,姓名,密码 from zgma where 编号 = '" & strusername & "' and 密码 = '" & strpassword & "'"
'response.write sql
rs.open sql, conn,1,1
if rs.recordcount <> 0 then
session("username")= strusername
response.write "登录成功!" & "<br>" ////连接串设错了,这一句也会执行
response.write "你的姓名是:" & rs("姓名") & "<br>"
else
response.write "登录不成功!" & "<br>"
response.write "no" & "<br>"
end if




rs.close
set rs=nothing
call closeconn

%>
</div>
</body>

</html>


...全文
155 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyzg_web 2005-05-10
  • 打赏
  • 举报
回复
字符型用%,数值型用=
--------------------------
谢谢,我不是问这个问题,我问的是为什么本来应该取不到记录集的,可是为什么用rs.recordcount判断还是判断出有记录呢?显示成功。应该显示失败才对呀?
即为什么当连接串错误时,还会生成记录集,而且用recordcount判断记录条数还是>0的。
我实在是不明白?
wyzg_web 2005-05-10
  • 打赏
  • 举报
回复
字符型用%,数值型用=
----------------------
为什么用like呢?
yb2008 2005-05-10
  • 打赏
  • 举报
回复
字符型用%,数值型用=


'------------------------------
楼主给分我哦...
yb2008 2005-05-10
  • 打赏
  • 举报
回复
sql = "select 编号,姓名,密码 from zgma where 编号 = '" & strusername & "' and 密码 = '" & strpassword & "'"

如果你的字段类型是字符型改成这样:


sql = "select 编号,姓名,密码 from zgma where 编号 like '" & strusername & "' and 密码 like '" & strpassword & "'"
wyzg_web 2005-05-10
  • 打赏
  • 举报
回复
我的语法肯定是没有问题的。
是不是不能用vb的思路写asp呀?
wyzg_web 2005-05-09
  • 打赏
  • 举报
回复
关中刀客的话不明白。再请教。
我就不信找不到。谢谢。
cmslovehxh 2005-05-09
  • 打赏
  • 举报
回复
你输入
' or 1=1--
就变成了
sql = "select 编号,姓名,密码 from zgma where 编号 = '' or 1=1--' and 密码 = '" & strpassword & "'"

也就是sql = "select 编号,姓名,密码 from zgma where 编号 = '' or 1=1"

cmslovehxh 2005-05-09
  • 打赏
  • 举报
回复
sql = "select 编号,姓名,密码 from zgma where 编号 = '" & strusername & "' and 密码 = '" & strpassword & "'"

用这个啊,太容易给人黑了。
wyzg_web 2005-05-09
  • 打赏
  • 举报
回复
楼上方法全试过,不行。
wyzg_web 2005-05-09
  • 打赏
  • 举报
回复
我试过不行,谢谢各位。
还要继续呀?
找问题。
Aspclass 2005-05-09
  • 打赏
  • 举报
回复
sql = "select 编号,姓名,密码 from zgma where 编号 = '" & strusername & "' and 密码 = '" & strpassword & "'"


改为

sql = "select 编号,姓名,密码 from zgma where 姓名= '" & strusername & "' and 密码 = '" & strpassword & "'"
Aspclass 2005-05-09
  • 打赏
  • 举报
回复
sql = "select 编号,姓名,密码 from zgma where 编号 = '" & strusername & "' and 密码 = '" & strpassword & "'"

这句有问题。。我认为你可能是在数据库手工设定的 姓名 为 1 或2......这类的。你的编号是ID编号
strusername 是姓名,可能ID 与姓名重复,比如说 只有在这种情况下他才能进入

编号 姓名 密码
1 1 1
2 2 2
jekexys2004 2005-05-09
  • 打赏
  • 举报
回复
有数据显示出来吗?应该是语法有问题
possible_Y 2005-05-09
  • 打赏
  • 举报
回复
rs.open sql, conn,1,1
1,1方式打开的rs.recordcount始终为-1(不管你连接字符串是否正确),当然不等于0啊

用if not rs.eof then判断
mkqiang 2005-05-09
  • 打赏
  • 举报
回复
rs.open sql, conn,1,1
if rs.recordcount <> 0 then
session("username")= strusername
... ...
你改为:
rs.open sql, conn,1,1
if not rs.eof then
... ...
再试试看。
wyzg_web 2005-05-09
  • 打赏
  • 举报
回复
肯定设错了,因为我是手工设错的。我故意把server写错了。可是当这种情况下,还会显示登录成功,但是是后面不显示人员名称了。
我就是不明白。我是用rs.recordcount判断的。可是为什么呢?
itzhiren 2005-05-09
  • 打赏
  • 举报
回复
你确定是连接串设错了吗?
wyzg_web 2005-05-09
  • 打赏
  • 举报
回复
我发现只要连接串不正确,if 语句就失去作用。总是执行if 后面的语句而不是执行else后面的语句。


---------------
1。菜手问:asp中如何使用on error goto语句呀?好象与vb中的使用方法不一致呀?
我知道on error resume next的用法。可是我不想忽略错误的存在,我想on error goto errlag
可是在asp脚本中报语法错误,使用有什么规定吗?是不是不能使用呀?
2。菜手问:如果连接串不正确,用server.createobject创建记录集对象也能成功,可是这会有很多隐患的。我想有一种检测机制,如果连接服务器不成功,则报错。不能创建记录集。可是如何实现呢?
请教各位老师了。


28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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