The connection cannot be used to perform this operation.
It is either closed or invalid in this context.
大家快来告诉我,这是什么错误?为什么会产生这种错误?如何解决?
快告诉我,否则我今天加班入要到深夜了!大慈大悲的师哥师姐,请快回答!

解决方案 »

  1.   

    Sub InitDBConn()
    Dim rs As New ADODB.Recordset
    Dim rst As New ADODB.Recordset
    Dim str1 As String
    Dim str2 As String
    On Error GoTo errorhandler
    rst.CursorType = adOpenKeyset
    rst.LockType = adLockOptimistic
    rst.Open "ChuciKaijiDatabase", C_cnn, , , adCmdTable
    If rst.RecordCount > 0 Then
       rst.MoveFirst
       If rst!初次开机 = True Then
         rst!初次开机 = False
         str1 = App.Path + "\SunBowTest_Database.mdb"
         rst.Update
       Else
        rs.CursorType = adOpenStatic
        rs.Open "DatabasemlDatabase", C_cnn, , , adCmdTable
        If rs.RecordCount > 0 Then
            rs.MoveFirst
            If IsNull(rs!数据库目录) Then
              str1 = App.Path + "\SunBowTest_Database.mdb"
            Else
              str1 = rs!数据库目录
            End If
        Else
            MsgBox "数据库路径错误!"
            str1 = App.Path + "\SunBowTest_Database.mdb"
            rs.Close
        End If
        rs.Close
        str2 = Right(str1, 23)
        If str2 <> "SunBowTest_Database.mdb" Then
            str1 = App.Path + "\SunBowTest_Database.mdb"
        End If
       End If
        
    Else
       str1 = App.Path + "\SunBowTest_Database.mdb"
    End If
    rst.Close
        P_cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=cghotel;Data Source=lry"
        P_cnn.Mode = adModeUnknown
        P_cnn.Open
        Exit Sub
    errorhandler:
        P_cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=cghotel;Data Source=lry"
        P_cnn.Open
        Exit Sub
    我没有关闭连接啊!
      

  2.   

    为什么上面是C_nn,下面是P_nn??
      

  3.   

    c_nn没有定义,更没有open,你让recordset怎么open??,先定义一个adodb.connection
    在连接就可以!
      

  4.   

    C_nn是它需要的连接,没有打开。
      

  5.   

    下面的P_cnn改成C_cnn ,并放在rst.open之前。
     P_cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=cghotel;Data Source=lry"
        P_cnn.Open