连接无法用于执行此操作。在此上下文中它可能已被关闭或无效
在ACTIVEX DLL里面放置窗体,加一个模块执行连接SQL
执行查询语句时报错了,如题
程序代码:
'连接SQL的模块 Public conn As ADODB.Connection Public rs As ADODB.Recordset Public addFlag As Boolean '声明部分 Public Function OpenCn(ByVal Cip As String, ByVal users As String, ByVal pw As String) As Boolean '连接模块 填写数据库等信息 'On Error GoTo err Set conn = New ADODB.Connection conn.ConnectionTimeout = 10 conn.Provider = "sqloledb" conn.Properties("data source").Value = Cip '服务器的名字 conn.Properties("initial catalog").Value = "DateCenter" '库名 'conn.Properties("integrated security").Value = "SSPI" '登陆类型 conn.Properties("user id").Value = users conn.Properties("Persist Security Info").Value = False conn.Properties("password").Value = pw '密码 ' conn.CursorLocation = adUseServer conn.Open OpenCn = True If conn.State = 1 Then addFlag = True Exit Function 'err: ' MsgBox err.Number & vbCrLf & err.Description, vbCritical End Function '关闭数据库,释放连接 Public Sub cloCn() On Error Resume Next If conn.State <> adStateClosed Then conn.Close Set conn = Nothing End Sub Public Function openRs(ByVal strsql As String) As Boolean '连接数据库记录集 Dim mag As String Dim rpy As Boolean 'On Error GoTo ERR Set rs = New ADODB.Recordset If addFlag = False Then rpy = True With rs .ActiveConnection = conn .CursorLocation = adUseClient .CursorType = adOpenKeyset .LockType = adLockOptimistic ' Debug.Print strsql .Open strsql End With addFlag = True openRs = True Exit Function 'ERR: ' ''调用自定议消息窗口 ' info ' openRs = False ' Exit Function '' End End Function Public Sub cloRs() 'On Error Resume Next If rs.State <> adStateClosed Then rs.Clone Set rs = Nothing '释放记录集 End Sub