代码如下:
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是把对象从内存中释放,是否已经包含中断与数据库的连接?

解决方案 »

  1.   

    应该是先rs.close再set rs=nothing
    另外,在每个窗体的unload事件中写上:set 窗体名=nothing
      

  2.   

    rs.close
    set rs=nothingcnn.closeset cnn=nothing
      

  3.   

    我把取得记录集写成模块级函数,方便随时调用,但只要我在该模块中取得记录集的函数里加入rs.close或cnn.close的话,就报:实时错误'3704' 对象关闭时,不允许操作。如果只是set nothing的话,就能正常运行,但不能close,我担心一直占用数据库服务器资源。请问到底在模块级编写数据库的操作如何释放资源,是否只要set nothing就行了?
      

  4.   

    我這樣寫的,參考一下吧.
    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
      

  5.   

    实时错误'3704' 对象关闭时,不允许操作
    ========
    这是因为你的rs、cnn对象已经关闭了,你再关就会报这个错。可以这样写:If rs.State = adStateOpen Then rs.Close
    Set rs = Nothing
    If cnn.State = adStateOpen Then cnn.Close
    Set cnn = Nothing