多谢各位高手,我这个菜鸟一个简单的问题,能帮忙看一下么,谢谢了。

王琦 2012-10-05 01:18:28
怎么下面这段代码在浏览器中运行后总是没反应呢,好像进入死循环了一样?

<%
yonghuming=request("yonghuming")
jiguan=request("jiguan")

'直连数据库 参数为sql,conn,rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={SQL Server};Server=(local);Database=jishubu;UID=sa;PWD="
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from geren"
rs.open sql,conn,3,3

rs.movefirst
while not rs.eof

if (rs("姓名"))=yonghuming then
'response.Write(jiguan)
'response.Write(rs("籍贯"))
rs("籍贯")=jiguan
rs.update()

end if

rs.movenext
wend
%>
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
王琦 2012-11-06
  • 打赏
  • 举报
回复
引用 2 楼 toury 的回复:
哦,忘记了重要的一点:你的代码中,在接受了传参后直接进行了数据库操作。这样不妥,如果参数没穿过来,后续根据参数做的任何操作都失去意义。所要要先判断参数是否传了过来: XML/HTML code 12345678910 <% yonghuming=request("yonghuming") jiguan=request("jiguan") if len(yo……
谢谢,谢谢,我已经解决了,多谢您。另外追问一句,“rs.update() ”这个update方法,是更新整个记录集,还是当前指针所在的这条记录集呢?
toury 2012-10-05
  • 打赏
  • 举报
回复
哦,忘记了重要的一点:你的代码中,在接受了传参后直接进行了数据库操作。这样不妥,如果参数没穿过来,后续根据参数做的任何操作都失去意义。所要要先判断参数是否传了过来:

<%
yonghuming=request("yonghuming")
jiguan=request("jiguan")
if len(yonghuming)>0 then
'do something here..............

end if

%>



toury 2012-10-05
  • 打赏
  • 举报
回复
关于“总是没反应呢,好像进入死循环”,解决思路:

1、页面输出
问题:你的代码中除了if (rs("姓名"))=yonghuming then 。。。end if里之外,其他没有任何输出。所以,如果条件不满足,页面当然不反应;
解决:在其他位置输出一些提示字符,如
<%
respinse.write("wangqi389的练习-------------><br>")
if (rs("姓名"))=yonghuming then 。。。end if
%>
2、数据库操作
问题:1)没有任何容错判断,造成你不知道数据库是否正确连接。如果没有连上,后续操作不就是无用功吗?呵呵;2)操作之后也应该做判断,看看数据库数据操作时是否有错误;3)记录集交叉操作:open了一个记录集rs,然后在循环里不停的用update方法写库,容易出错;4)即使释放数据库占用的资源,做到即用即开、用后关闭;
解决:
1)加入必要的判断
<%
err.clear
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "....................."
if conn.state<>1 then
response.write("数据库没连上啊~~~~")
//response.write(err.description)//仅在调试代码时使用。以后要注释掉
set conn=nothing//释放资源
response.end()//终止程序运行,避免后续的不正确操作
end if
%>
2)加入必要的判断
<%
if (rs("姓名"))=yonghuming then
'response.Write(jiguan)
'response.Write(rs("籍贯"))
rs("籍贯")=jiguan
err.clear
rs.update()
if err<>0 then
response.write("<script>alert('success!')</script>")
//response.write(err.description)//仅在调试代码时使用。以后要注释掉
else
response.write("<script>alert('failed!')</script>")
end if
%>
3)使用conn的UPDATE语句取代rs.update
<%
if (rs("姓名"))=yonghuming then
err.clear
if len(jiguan)>0 then
conn.execute("UPDATE [geren] SET [籍贯]='"&jiguan&"'")
end if
if err<>0 then
response.write("<script>alert('success!')</script>")
//response.write(err.description)//仅在调试代码时使用。以后要注释掉
else
response.write("<script>alert('failed!')</script>")
end if
end if
%>
4)释放资源
在确认不需要数据库操作的地方:
<%
if rs.state<>0 then rs.close
set rs=nothing
if conn.state<>0 then conn.close
set conn=nothing

%>

随手敲的代码,有错难免。主要是给个参考。
  • 打赏
  • 举报
回复
楼主给出了一个最糟糕代码的示范

<%
yonghuming=request("yonghuming")
jiguan=request("jiguan")
在此应验证这两个变量是否为空,进行相应错误处理。
在此应对变量内容进行SQL特殊字符过滤,防止SQL注入。
'直连数据库 参数为sql,conn,rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={SQL Server};Server=(local);Database=jishubu;UID=sa;PWD=口令"

程序为了更新一个籍贯,使用了非常低效的方法。
---------------------------------
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from geren"
rs.open sql,conn,3,3
rs.movefirst
while not rs.eof
if (rs("姓名"))=yonghuming then
'response.Write(jiguan)
'response.Write(rs("籍贯"))
rs("籍贯")=jiguan
rs.update()
end if
rs.movenext
wend
---------------------------------

实际上只要一条语句就够了
conn.execute("update geren set 籍贯='"+jiguan+"' where 姓名='"+yonghuming+"'")

另外结束时应该关闭conn
conn.Close

%>

28,391

社区成员

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

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