请求的操作需要OLE DB会话对象 - VB6到SQL Server 2000

时间:2013-07-11 15:41:55

标签: sql-server vb6

我正试图进入一个大约十年前为客户编写的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工作,我有点不知所措。我无法弄清楚为什么它有时会起作用,而不是其他人。如果有人有任何见解,他们将非常感激。

1 个答案:

答案 0 :(得分:0)

更改错误处理以更好地了解正在发生的事情。由于您将新的对象(设置m_DBConnection = New ADODB.Connection)设置为新对象,因此检查(m_DBConnection Is Nothing)不会做太多事情,因为ojbect肯定已经存在。