我试图进入大约十年前为客户编写的 VB6 应用程序,但我间歇性地不断收到此错误。该应用程序在启动时需要登录,并且在输入为我提供的登录名后(我 100% 确定它是正确的),出现以下错误:
运行时错误“3709”
请求的操作需要当前提供程序不支持的 OLE DB 会话对象。
真正奇怪的是,昨晚我能够毫无问题地登录。但是,大约一周前我遇到了这个问题,但是我出城了几天,当我回来时,我可以再次登录。在那个初始实例之前,我能够正常登录。我注意到已经发布了一个类似的问题,但是给出的解决方案对我不起作用。这是与建立数据库连接有关的代码。请注意,Serv1、Use1、PW1 等只是服务器名称/用户名/密码的填充物。
Public Function GetConnected()
' This function decides which server to connect and makes the connection
'Determines which connection string to use
If frmSplash.Text1 = "1" Or frmSplash.Text1 = "apc" Then 'server location
'determines if the logon contains '1' or 'apc'
'APC connection code
strSQLServerName = "(Serv1)"
strSQLDBUserName = "Use1"
strSQLDBPassword = "PW1"
strSQLPort = ""
ElseIf frmSplash.Text1 = "2" Then
'Laptop connection string
strSQLServerName = "(Serv1)"
strSQLDBUserName = "Use2"
strSQLDBPassword = "PW2"
strSQLPort = ""
Else
'Client connection code
strSQLServerName = "Serv2
strSQLDBUserName = "Use3"
strSQLDBPassword = "PW3"
strSQLPort = ""
End If 'server location
'If (m_DBConnection Is Nothing) Then
Set m_DBConnection = New ADODB.Connection
'End If
SessionLocation = frmSplash.LocationCombo.Text
'***************************************
'Connecs to database based on location
If frmSplash.LocationCombo.Text = "Loc1" Then
strSQLDBName = "ServLoc1"
ElseIf frmSplash.LocationCombo.Text = "Loc2" Then
strSQLDBName = "ServLoc2"
Else
strSQLDBName = "ServLoc3"
End If
'**************************
'Builds connection string
m_DBConnection.ConnectionString = "Provider=SQLOLEDB;" & _
"Data Source = '" & strSQLServerName & strSQLPort & "';" & _
"uid=" & strSQLDBUserName & ";" & _
"pwd=" & strSQLDBPassword & ";" & _
"Database=" & strSQLDBName & ";"
On Error GoTo errorhandler
m_DBConnection.Open
If (m_DBConnection Is Nothing) Then
MsgBox "Connection Failed"
End If
Exit Function
errorhandler:
MsgBox ("Problem with the Server")
'MsgBox "Connection State " & GetState(m_DBConnection.State)
End Function
Public Function ExecuteSQL(strSQL As String) As ADODB.Recordset
'Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
**cmd.ActiveConnection = m_DBConnection** <-----(Error occurs here)
cmd.CommandType = adCmdText
cmd.CommandText = strSQL
Set ExecuteSQL = cmd.Execute
Exit Function
变量定义:
Public strSQLServerName 'Holds the name of the SQL Server
Public strSQLDBUserName 'Holds the user name (for SQL Server Authentication)
Public strSQLDBPassword 'Holds the password (for SQL Server Authentication)
Public strSQLDBName 'Holds name of a database on the server
Public strSQLPort 'Holds the Port Number
Public SessionUser As Integer ' To Track the type of User (3 Levels)
Public SessionLocation As String ' To Track the DB throughout the Session
Public m_DBConnection As ADODB.Connection
Public cmd As ADODB.Command
这是我第一次在 VB6 中工作,我有点不知所措。我无法弄清楚为什么它有时会起作用,而有时却不起作用。如果有人有任何见解,他们将不胜感激。