代码如下:
Dim cnn as new adodb.connection
Dim rs as new adodb.recordset
.....
set rs=nothing
rs.close
set cnn=nothing
cnn.close请问定义了数据库连接与记录集使用后如何释放资源?以上代码正确吗?set nothing后是否还要close?set nothing是把对象从内存中释放,是否已经包含中断与数据库的连接?
Dim cnn as new adodb.connection
Dim rs as new adodb.recordset
.....
set rs=nothing
rs.close
set cnn=nothing
cnn.close请问定义了数据库连接与记录集使用后如何释放资源?以上代码正确吗?set nothing后是否还要close?set nothing是把对象从内存中释放,是否已经包含中断与数据库的连接?
另外,在每个窗体的unload事件中写上:set 窗体名=nothing
set rs=nothingcnn.closeset cnn=nothing
Public Function OpenDBase(OpenStr As String) As Boolean
OpenDBase = False
On Error GoTo ErrX
Screen.MousePointer = 11
If ConnectToServer = False Then
GoTo ErrX
End If
If D1.DataConnect.State = 1 Then
D1.DataConnect.CommitTrans
D1.DataConnect.Close
End If
D1.DataConnect.Open SystemLjStr, LogName, LogPass
If x_RS.State = 1 Then
x_RS.Close
Set x_RS = Nothing
End If
Set x_RS = New ADODB.Recordset
x_RS.Open OpenStr, D1.DataConnect, adOpenKeyset, adLockBatchOptimistic
Screen.MousePointer = 0
OpenDBase = True
Exit Function
ErrX:
If Err.Number = -2147168242 Then
Resume Next
End If
Screen.MousePointer = 0
Call Xtxxts("帳套數据庫打開失敗!", 0, 1)
Exit Function
End Function
========
这是因为你的rs、cnn对象已经关闭了,你再关就会报这个错。可以这样写:If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
If cnn.State = adStateOpen Then cnn.Close
Set cnn = Nothing