提问个很古老的问题!关于conn的关闭与注销!

Lee2K 2008-11-30 10:17:24
为我们在asp使用中,在使用数据库链接后,通常都使用:
conn.close
set conn=nothing
来关闭数据链接与注销他,避免浪费不必要的资源.

嗯...问个比较怪的,在Response.Redirect之前,如果我们有打开过数据,是不是不用关闭conn也没事?

如下两个代码.两个正确点?或者是,还有更好的方法?

set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from [abc] where [id]=1"
rs.open sql,conn,1,2
If rs.recordcount<>0 Then
response.write("- -!")
Else
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.Redirect("index.asp")
Response.End
End If


set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from [abc] where [id]=1"
rs.open sql,conn,1,2
If rs.recordcount<>0 Then
response.write("- -!")
Else
Response.Redirect("index.asp")
Response.End
End If
rs.close
set rs=nothing
conn.close
set conn=nothing
...全文
61 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lee2K 2008-12-01
  • 打赏
  • 举报
回复
GOOD.我决定把分数都给你.谢谢.
Lee2K 2008-11-30
  • 打赏
  • 举报
回复
嗯...同意...但那是临时打上去的.没有注意.嘿嘿.
Yes1000 2008-11-30
  • 打赏
  • 举报
回复
你的两段代码都有问题,第一段,如果rs.recordcount<>0则数据库连接不会被关闭
第二段代码,如果如果rs.recordcount=0则数据库连接不会被关闭
Yes1000 2008-11-30
  • 打赏
  • 举报
回复
所以如果有数据库连接,要在Response.Redirect之前关闭掉。
如果Response.Redirect在分支里,要在分支里关闭数据库连接。 代码如上贴
Lee2K 2008-11-30
  • 打赏
  • 举报
回复
说得也是啊...
Yes1000 2008-11-30
  • 打赏
  • 举报
回复


Response.Redirect之后的任何代码都是不会被执行的,因为还没有执行页面就已经跳转了。Response.End都不用写

这样更好:
    set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from [abc] where [id]=1"
rs.open sql,conn,1,2
If rs.recordcount<>0 Then
response.write("- -!")
rs.close
set rs=nothing
conn.close
set conn=nothing

Else
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.Redirect("index.asp")
End If

或者:
    set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from [abc] where [id]=1"
rs.open sql,conn,1,2
If rs.recordcount<>0 Then
response.write("- -!")
Else
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.Redirect("index.asp")
End If
rs.close
set rs=nothing
conn.close
set conn=nothing

28,409

社区成员

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

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