该连接不能用于执行此操作。 vb6 中的此上下文错误可能已关闭或无效
我正在尝试执行将记录集值存储在 sql 数据库中的查询。当我尝试执行时,我收到错误,例如
连接无法用于执行此操作。 vb6 中的此上下文错误可能已关闭或无效。请帮我解决这个问题。
' Write records to Database
frmDNELoad.lblStatus.Caption = "Loading data into database......"
Call FindServerConnection_NoMsg
Dim lngRecCount As Long
lngRecCount = 0
rcdDNE.MoveFirst
Set rcdReclamation = New ADODB.Recordset
With rcdReclamation
.ActiveConnection = objConn
.Source = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" & rcdDNE("RTN") & "', '" & rcdDNE("AccountNbr") & "', '" & rcdDNE("FirstName") & "', '" & rcdDNE("MiddleName") & "', '" & rcdDNE("LastName") & "', '" & rcdDNE("Amount") & "')"
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmdCommand
End With
Do Until rcdDNE.EOF
lngRecCount = lngRecCount + 1
frmDNELoad.lblStatus.Caption = "Adding record " & lngRecCount & " of " & rcdDNE.RecordCount & " to database."
frmDNELoad.Refresh
DoEvents
Call CommitNew
rcdDNE.MoveNext
Loop
frmDNELoad.lblStatus.Caption = "DNE Processing Complete."
frmDNELoad.Refresh
End Function
Sub CommitNew()
' Add records to DneFrc table
With rcdReclamation
.Requery
.AddNew
.Fields![RTN] = rcdDNE.Fields![RTN]
.Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
.Fields![FirstName] = rcdDNE.Fields![FirstName]
.Fields![MiddleName] = rcdDNE.Fields![MiddleName]
.Fields![LastName] = rcdDNE.Fields![LastName]
.Fields![Amount] = rcdDNE.Fields![Amount]
.Update
End With
End Sub
连接代码
Sub InstantiateCommand_SQLText() ' Creates a command object to be used when executing SQL statements. Set objCommSQLText = New ADODB.Command objCommSQLText.ActiveConnection = objConn objCommSQLText.CommandType = adCmdText End Sub Function FindServerConnection_NoMsg() As String Dim rcdClientPaths As ADODB.Recordset Dim strDBTemp As String Const CLIENT_UPDATE_DIR = "\\PSGSPHX02\NORS\Rs\ClientUpdate\" On Error Resume Next ' If persisted recordset is not there, try and copy one down from ' CLIENT_UPDATE_DIR. If that can't be found, create a blank one ' and ask the user for the server name. Set rcdClientPaths = New ADODB.Recordset ' Does it already exist locally? If FileExists_FullPath(App.Path & "\" & "t_PCD_ServerConnectionList.xml") = False Then ' Can it be retrieved from CLIENT_UPDATE_DIR If Dir(CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml") "" Then FileCopy CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml", App.Path & "\" & "t_PCD_ServerConnectionList.xml" Else ' Creat a blank one. With rcdClientPaths .Fields.Append "ServerConnection", adVarChar, 250 .Fields.Append "Description", adVarChar, 50 .CursorType = adOpenDynamic .LockType = adLockOptimistic .CursorLocation = adUseClient .Open .Save App.Path & "\" & "t_PCD_ServerConnectionList.xml", adPersistXML .Close End With End If End If ' Open the recordset With rcdClientPaths .CursorType = adOpenDynamic .LockType = adLockOptimistic .CursorLocation = adUseClient .Open App.Path & "\" & "t_PCD_ServerConnectionList.xml", , , , adCmdFile End With If rcdClientPaths.RecordCount 0 Then ' try each one listed rcdClientPaths.MoveFirst Do Until rcdClientPaths.EOF strDBTemp = TryConnection_NoMsg(rcdClientPaths.Fields![serverconnection]) If strDBTemp "" Then FindServerConnection_NoMsg = strDBTemp Exit Function End If rcdClientPaths.MoveNext Loop strDBTemp = "" End If Do While strDBTemp = "" If strDBTemp "" Then strDBTemp = TryConnection_NoMsg(strDBTemp) If strDBTemp "" Then With rcdClientPaths .AddNew .Fields![serverconnection] = strDBTemp .Update .Save End With FindServerConnection_NoMsg = strDBTemp Exit Function End If Else Exit Function End If Loop End Function Function TryConnection_NoMsg(ByVal SvName As String) As String On Error GoTo ErrHandle ' If a server was provided, try to open a connection to it. Screen.MousePointer = vbHourglass Set objConn = New ADODB.Connection With objConn .CommandTimeout = 30 .ConnectionTimeout = 30 .ConnectionString = "Provider=SQLOLEDB.1; Server=" & SvName & "; User ID=RS_Auth; Password=weLcomers_auth; Initial Catalog=NORS" ' Test .Open .Close End With Set objConn = Nothing TryConnection_NoMsg = SvName Screen.MousePointer = vbNormal Exit Function ErrHandle: TryConnection_NoMsg = "" Set objConn = Nothing Screen.MousePointer = vbNormal Exit Function End Function
I am trying to execute the query which stores recordset vales in sql db. when I am trying to execute that i am getting error like
the connection cannt be used to perform this operation. It may closed or not valid in this context error in vb6. Please help me to solve this issue.
' Write records to Database
frmDNELoad.lblStatus.Caption = "Loading data into database......"
Call FindServerConnection_NoMsg
Dim lngRecCount As Long
lngRecCount = 0
rcdDNE.MoveFirst
Set rcdReclamation = New ADODB.Recordset
With rcdReclamation
.ActiveConnection = objConn
.Source = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" & rcdDNE("RTN") & "', '" & rcdDNE("AccountNbr") & "', '" & rcdDNE("FirstName") & "', '" & rcdDNE("MiddleName") & "', '" & rcdDNE("LastName") & "', '" & rcdDNE("Amount") & "')"
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmdCommand
End With
Do Until rcdDNE.EOF
lngRecCount = lngRecCount + 1
frmDNELoad.lblStatus.Caption = "Adding record " & lngRecCount & " of " & rcdDNE.RecordCount & " to database."
frmDNELoad.Refresh
DoEvents
Call CommitNew
rcdDNE.MoveNext
Loop
frmDNELoad.lblStatus.Caption = "DNE Processing Complete."
frmDNELoad.Refresh
End Function
Sub CommitNew()
' Add records to DneFrc table
With rcdReclamation
.Requery
.AddNew
.Fields![RTN] = rcdDNE.Fields![RTN]
.Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
.Fields![FirstName] = rcdDNE.Fields![FirstName]
.Fields![MiddleName] = rcdDNE.Fields![MiddleName]
.Fields![LastName] = rcdDNE.Fields![LastName]
.Fields![Amount] = rcdDNE.Fields![Amount]
.Update
End With
End Sub
conection code
Sub InstantiateCommand_SQLText() ' Creates a command object to be used when executing SQL statements. Set objCommSQLText = New ADODB.Command objCommSQLText.ActiveConnection = objConn objCommSQLText.CommandType = adCmdText End Sub Function FindServerConnection_NoMsg() As String Dim rcdClientPaths As ADODB.Recordset Dim strDBTemp As String Const CLIENT_UPDATE_DIR = "\\PSGSPHX02\NORS\Rs\ClientUpdate\" On Error Resume Next ' If persisted recordset is not there, try and copy one down from ' CLIENT_UPDATE_DIR. If that can't be found, create a blank one ' and ask the user for the server name. Set rcdClientPaths = New ADODB.Recordset ' Does it already exist locally? If FileExists_FullPath(App.Path & "\" & "t_PCD_ServerConnectionList.xml") = False Then ' Can it be retrieved from CLIENT_UPDATE_DIR If Dir(CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml") "" Then FileCopy CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml", App.Path & "\" & "t_PCD_ServerConnectionList.xml" Else ' Creat a blank one. With rcdClientPaths .Fields.Append "ServerConnection", adVarChar, 250 .Fields.Append "Description", adVarChar, 50 .CursorType = adOpenDynamic .LockType = adLockOptimistic .CursorLocation = adUseClient .Open .Save App.Path & "\" & "t_PCD_ServerConnectionList.xml", adPersistXML .Close End With End If End If ' Open the recordset With rcdClientPaths .CursorType = adOpenDynamic .LockType = adLockOptimistic .CursorLocation = adUseClient .Open App.Path & "\" & "t_PCD_ServerConnectionList.xml", , , , adCmdFile End With If rcdClientPaths.RecordCount 0 Then ' try each one listed rcdClientPaths.MoveFirst Do Until rcdClientPaths.EOF strDBTemp = TryConnection_NoMsg(rcdClientPaths.Fields![serverconnection]) If strDBTemp "" Then FindServerConnection_NoMsg = strDBTemp Exit Function End If rcdClientPaths.MoveNext Loop strDBTemp = "" End If Do While strDBTemp = "" If strDBTemp "" Then strDBTemp = TryConnection_NoMsg(strDBTemp) If strDBTemp "" Then With rcdClientPaths .AddNew .Fields![serverconnection] = strDBTemp .Update .Save End With FindServerConnection_NoMsg = strDBTemp Exit Function End If Else Exit Function End If Loop End Function Function TryConnection_NoMsg(ByVal SvName As String) As String On Error GoTo ErrHandle ' If a server was provided, try to open a connection to it. Screen.MousePointer = vbHourglass Set objConn = New ADODB.Connection With objConn .CommandTimeout = 30 .ConnectionTimeout = 30 .ConnectionString = "Provider=SQLOLEDB.1; Server=" & SvName & "; User ID=RS_Auth; Password=weLcomers_auth; Initial Catalog=NORS" ' Test .Open .Close End With Set objConn = Nothing TryConnection_NoMsg = SvName Screen.MousePointer = vbNormal Exit Function ErrHandle: TryConnection_NoMsg = "" Set objConn = Nothing Screen.MousePointer = vbNormal Exit Function End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经在
TryConnection_NoMsg
函数中关闭了连接(?)You have already closed the connection here in
TryConnection_NoMsg
function (?)我怀疑
FindServerConnection_NoMsg
无法打开连接,并且由于它以NoMsg
结尾,因此您没有看到有关连接未打开原因的错误。然后您继续使用该连接,而不知道打开失败。发布
FindServerConnection_NoMsg
的代码。顺便说一句,你的问题本身应该给你一个线索。它特别指出该连接无法使用,并且可能未打开。这应该告诉您从哪里开始寻找,并且至少告诉您应该将打开连接的代码作为问题的一部分发布。
I'd suspect that
FindServerConnection_NoMsg
is not managing to open the connection, and since it ends inNoMsg
that you're not seeing the error about why the connection wasn't opened. You then go on to just use the connection without knowing that the open failed.Post the code for
FindServerConnection_NoMsg
.BTW, your question itself should have given you a clue. It specifically says that the connection can't be used, and that it may not be open. That should have told you where to start looking, and at the least told you you should have posted the code that opened the connection as part of your question.
谢谢大家。我解决了我的问题。这就是我在代码
Dim lngRecCount As Long 中所改变的
lngRecCount = 0
rcdDNE.MoveFirst
Thanks for everyone. I sloved my problem. This what i cahnge in my code
Dim lngRecCount As Long
lngRecCount = 0
rcdDNE.MoveFirst