阅读: 2 评论: 0 作者: eng308 发表于 2009-12-14 11:05 原文链接
1、类的基本信息
代码
<%
Class CacheConn
Public Conn ' 数据连接对像
Private ErrStr ' 出错字符串
Private DBcache ' 是否使用dbcheck
Private ConFlag ' 数据连接状态
Private CookieName ' CookIe名(用于防止多网站冲突)
Private ConnStr ' 连接数据字串
' 类始化
Private Sub Class_Initialize()
DBcache = 1
ConFlag = 0
End Sub
' 类注销
Private Sub Class_Terminate()
CloseDatabase
End Sub
Public Property Let inCookieName(Vstr) ' CookIe名(用于防止多网站冲突)
CookieName = CStr(Vstr)
End Property
Public Property Let inConnStr(Vstr) ' 连接数据字串
ConnStr = CStr(Vstr)
End Property
Public Property Let inDBcache(Vstr) ' 连接数据字串
DBcache = CLng(Vstr)
End Property
' 打开数据连接
Public Sub OpenDatabase()
If DBcache = 1 Then
If IsObject(Application(CookieName & " conn " )) = False Or Application(CookieName & " conn " ) = "" Or Application(CookieName & " conn " ) <> Empty Then
Set Conn = Server.CreateObject( " ADODB.Connection " )
Conn.ConnectionString = ConnStr
Conn.Open
If Err Then
Err.Clear
Set Conn = Nothing
ErrStr = ErrStr & " 数据库连接错误!<br> " & vbCrLf
Response.Write ErrStr
Response.End
End If
Application.Lock
Set Application(CookieName & " conn " ) = Conn
Application.UnLock
Else
Set Conn = Application(CookieName & " conn " )
End If
Else
Set Conn = Server.CreateObject( " ADODB.Connection " )
Conn.ConnectionString = ConnStr
Conn.Open
If Err Then
Err.Clear
Set Conn = Nothing
ErrStr = ErrStr & " 数据库连接错误!<br> " & vbCrLf
Response.Write ErrStr
Response.End
End If
End If
ConFlag = 1
End Sub
' 关闭数据连接
Public Sub CloseDatabase()
If ConFlag = 0 Then Exit Sub
If DBcache = 1 Then
Set Conn = Nothing
Else
Conn.Close
Set Conn = Nothing
End If
ConFlag = 0
End Sub
End Class
%>
Class CacheConn
Public Conn ' 数据连接对像
Private ErrStr ' 出错字符串
Private DBcache ' 是否使用dbcheck
Private ConFlag ' 数据连接状态
Private CookieName ' CookIe名(用于防止多网站冲突)
Private ConnStr ' 连接数据字串
' 类始化
Private Sub Class_Initialize()
DBcache = 1
ConFlag = 0
End Sub
' 类注销
Private Sub Class_Terminate()
CloseDatabase
End Sub
Public Property Let inCookieName(Vstr) ' CookIe名(用于防止多网站冲突)
CookieName = CStr(Vstr)
End Property
Public Property Let inConnStr(Vstr) ' 连接数据字串
ConnStr = CStr(Vstr)
End Property
Public Property Let inDBcache(Vstr) ' 连接数据字串
DBcache = CLng(Vstr)
End Property
' 打开数据连接
Public Sub OpenDatabase()
If DBcache = 1 Then
If IsObject(Application(CookieName & " conn " )) = False Or Application(CookieName & " conn " ) = "" Or Application(CookieName & " conn " ) <> Empty Then
Set Conn = Server.CreateObject( " ADODB.Connection " )
Conn.ConnectionString = ConnStr
Conn.Open
If Err Then
Err.Clear
Set Conn = Nothing
ErrStr = ErrStr & " 数据库连接错误!<br> " & vbCrLf
Response.Write ErrStr
Response.End
End If
Application.Lock
Set Application(CookieName & " conn " ) = Conn
Application.UnLock
Else
Set Conn = Application(CookieName & " conn " )
End If
Else
Set Conn = Server.CreateObject( " ADODB.Connection " )
Conn.ConnectionString = ConnStr
Conn.Open
If Err Then
Err.Clear
Set Conn = Nothing
ErrStr = ErrStr & " 数据库连接错误!<br> " & vbCrLf
Response.Write ErrStr
Response.End
End If
End If
ConFlag = 1
End Sub
' 关闭数据连接
Public Sub CloseDatabase()
If ConFlag = 0 Then Exit Sub
If DBcache = 1 Then
Set Conn = Nothing
Else
Conn.Close
Set Conn = Nothing
End If
ConFlag = 0
End Sub
End Class
%>
2、使用方法
代码
<%
Dim GBL_ConnStr ' 连接数据语句
GBL_ConnStr = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.Mappath( " data/domain.mdb " )
Const GBL_CookieName = " x86fly38domainmanager " ' 定义缓存名称(防止多网站冲突)
Const GBL_DBcache = 1 ' 定义是否使用缓存
Dim Conn
' 打开数据
Sub inDatabase
' On Error Resume Next
Set eCon = New CacheConn
eCon.inCookieName = GBL_CookieName
eCon.inConnStr = GBL_ConnStr
eCon.inDBcache = GBL_DBcache
eCon.OpenDatabase
Set Conn = eCon.Conn
End Sub
' 关闭数据
Sub OutDatabase
Set eCon = Nothing
End Sub
%>
Dim GBL_ConnStr ' 连接数据语句
GBL_ConnStr = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Server.Mappath( " data/domain.mdb " )
Const GBL_CookieName = " x86fly38domainmanager " ' 定义缓存名称(防止多网站冲突)
Const GBL_DBcache = 1 ' 定义是否使用缓存
Dim Conn
' 打开数据
Sub inDatabase
' On Error Resume Next
Set eCon = New CacheConn
eCon.inCookieName = GBL_CookieName
eCon.inConnStr = GBL_ConnStr
eCon.inDBcache = GBL_DBcache
eCon.OpenDatabase
Set Conn = eCon.Conn
End Sub
' 关闭数据
Sub OutDatabase
Set eCon = Nothing
End Sub
%>
最新新闻:
· 传盛大密签王菲 春晚实为游戏电影造势(2010-02-06 12:28)
· 商业周刊:乔布斯iPad志在云计算(2010-02-06 12:27)
· 十大最佳Ajax教程(2010-02-06 12:21)
· 微软下周不修复新IE漏洞(2010-02-06 12:18)
· Firefox 4.0演示视频曝光(2010-02-06 12:10)