[原创] 自动关闭数据库连接 Conn
在我们写ASP程序的过程中,总有一些粗心的程序员会忘记关掉数据库连接。
这样会浪费服务器的资源。
我试了试,通过如下的方法可以实现Conn对象的自动关闭:
代码如下:
'' 文件:Conn.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'option explicit
dim dataname,starttime,conn,db
response.buffer=true
starttime=timer()*1000
Dim connstr
db="testdb.mdb"
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
On Error Resume Next
class ClsConn
Private Sub Class_Initialize()
On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "Sorry! 数据库连接出错,请检查连接字串。"
Response.End
End If
Response.write("conn Open")
End Sub
Private Sub Class_Terminate()
if isobject(conn) then
conn.close()
Response.write("conn Close")
end if
End Sub
end class
dim objClsConn
set objClsConn= new ClsConn
%>
<%
'option explicit
dim dataname,starttime,conn,db
response.buffer=true
starttime=timer()*1000
Dim connstr
db="testdb.mdb"
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
On Error Resume Next
class ClsConn
Private Sub Class_Initialize()
On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "Sorry! 数据库连接出错,请检查连接字串。"
Response.End
End If
Response.write("conn Open")
End Sub
Private Sub Class_Terminate()
if isobject(conn) then
conn.close()
Response.write("conn Close")
end if
End Sub
end class
dim objClsConn
set objClsConn= new ClsConn
%>
QQ:273352165
evlon#126.com
转载请注明出处。