Public Function isConnection(Optional ByVal sConnect As String) As Boolean Dim oConn As Object Dim oRecordset As Object Dim lErrNo As Long
On Error GoTo ErrorHandler
Set oConn = New ADODB.Connection Set oRecordset = New ADODB.Recordset
If Len(sConnect) Then oConn.Open sConnect oConn.Close End If Set oRecordset = Nothing Set oConn = Nothing isConnection = True Exit FunctionErrorHandler:
lErrNo = Err.Number
If lErrNo = [can't connectted] Then isConnection = False End If Set oRecordset = Nothing Set oConn = Nothing
End Function
if connection.state = adStateOpen then '数据库的当前状态是打开的 elseif connection.state = asStateClosed then '数据库的当前状态是关闭的 end if
connection的属性state当(state and 0)=true,关闭 (state and 1)=true,打开 (state and 2)=true,正在打开 (state and 4)=true,查询正在进行中 (state and 8)=true,正在执行或打开对象(因此对象不可用) WWW @-@ ^
connection.state=1(断开)
'此连接是关闭的
else
'此连接是打开的
endif
connection.state=1(连接)
connection.state=0(断开)
如果出现一般性网络错误的话,数据库的连接将断开,此时connection.state仍然是adStateOpen,即1。请问如何判断connection是否已经和数据库断开?
谢谢!
Dim oConn As Object
Dim oRecordset As Object
Dim lErrNo As Long
On Error GoTo ErrorHandler
Set oConn = New ADODB.Connection
Set oRecordset = New ADODB.Recordset
If Len(sConnect) Then
oConn.Open sConnect
oConn.Close
End If Set oRecordset = Nothing
Set oConn = Nothing isConnection = True
Exit FunctionErrorHandler:
lErrNo = Err.Number
If lErrNo = [can't connectted] Then
isConnection = False
End If
Set oRecordset = Nothing
Set oConn = Nothing
End Function
'数据库的当前状态是打开的
elseif connection.state = asStateClosed then
'数据库的当前状态是关闭的
end if
http://www.csdn.net/expert/topic/900/900702.xml?temp=.3095514
不成功的一次就为断开的状态。
输出相关信息。
(state and 1)=true,打开
(state and 2)=true,正在打开
(state and 4)=true,查询正在进行中
(state and 8)=true,正在执行或打开对象(因此对象不可用) WWW
@-@
^