我在一些excel过程中使用以下内容来建立与我们的数据库的连接.
Private Const strConn As String = _
"PROVIDER=SQLOLEDB.1 ..."
Sub OpenConnection()
Set cn = CreateObject("ADODB.Connection")
cn.Open strConn
cn.CommandTimeout = 0
Set rs = CreateObject("ADODB.Recordset")
Set rs.ActiveConnection = cn
End Sub
Run Code Online (Sandbox Code Playgroud)
在后续代码中,我使用各种SQL字符串打开连接.
我想测试是否rs
打开所以我知道它需要关闭,但以下不起作用.如何更改以下条件才能工作?
If (rs.Open = True) Then
rs.Close
End If
Run Code Online (Sandbox Code Playgroud)
以下工作,但我不想以这种方式使用错误捕获:
On Error Resume Next
rs.Close
Run Code Online (Sandbox Code Playgroud)
Ray*_*arg 32
ADO记录集.State
属性,你可以检查其值adStateClosed
或adStateOpen
If Not (rs Is Nothing) Then
If (rs.State And adStateOpen) = adStateOpen Then rs.Close
Set rs = Nothing
End If
Run Code Online (Sandbox Code Playgroud)
编辑; 不检查.State
1或0的原因是因为即使它在99.99%的时间内工作,仍然可以设置其他标志,这将导致If语句未通过adStateOpen
检查.
EDIT2:
对于未引用ActiveX数据对象的后期绑定,您几乎没有选项.使用ObjectStateEnum中的adStateOpen常量值
If Not (rs Is Nothing) Then
If (rs.State And 1) = 1 Then rs.Close
Set rs = Nothing
End If
Run Code Online (Sandbox Code Playgroud)
或者您可以自己定义常量以使代码更具可读性(将它们全部定义为一个很好的示例.)
Const adStateClosed As Long = 0 'Indicates that the object is closed.
Const adStateOpen As Long = 1 'Indicates that the object is open.
Const adStateConnecting As Long = 2 'Indicates that the object is connecting.
Const adStateExecuting As Long = 4 'Indicates that the object is executing a command.
Const adStateFetching As Long = 8 'Indicates that the rows of the object are being retrieved.
[...]
If Not (rs Is Nothing) Then
' ex. If (0001 And 0001) = 0001 (only open flag) -> true
' ex. If (1001 And 0001) = 0001 (open and retrieve) -> true
' This second example means it is open, but its value is not 1
' and If rs.State = 1 -> false, even though it is open
If (rs.State And adStateOpen) = adStateOpen Then
rs.Close
End If
Set rs = Nothing
End If
Run Code Online (Sandbox Code Playgroud)
这是一个老话题,但以防万一其他人仍在寻找......
我在脱离事件后遇到了麻烦。即使重新连接到网络后,保存在全局对象中的打开的数据库连接也会出错。这是由于 TCP 连接被远程主机强制终止。(错误-2147467259:TCP提供程序:现有连接被远程主机强制关闭。)
但是,该错误仅在尝试第一笔交易后才会显示。到目前为止,Connection.State 和 Connection.Version(根据上述解决方案)都不会显示任何错误。
所以我写了下面的小子来强制错误 - 希望它有用。
我的设置(Access 2016、SQL Svr 2008R2)的性能测试每次调用大约需要 0.5 毫秒。
Function adoIsConnected(adoCn As ADODB.Connection) As Boolean
'----------------------------------------------------------------
'#PURPOSE: Checks whether the supplied db connection is alive and
' hasn't had it's TCP connection forcibly closed by remote
' host, for example, as happens during an undock event
'#RETURNS: True if the supplied db is connected and error-free,
' False otherwise
'#AUTHOR: Belladonna
'----------------------------------------------------------------
Dim i As Long
Dim cmd As New ADODB.Command
'Set up SQL command to return 1
cmd.CommandText = "SELECT 1"
cmd.ActiveConnection = adoCn
'Run a simple query, to test the connection
On Error Resume Next
i = cmd.Execute.Fields(0)
On Error GoTo 0
'Tidy up
Set cmd = Nothing
'If i is 1, connection is open
If i = 1 Then
adoIsConnected = True
Else
adoIsConnected = False
End If
End Function
Run Code Online (Sandbox Code Playgroud)