2013-07-11 105 views
1

我試圖進入一個VB6應用程序,這個應用程序爲大約十年前的客戶端編寫,但是間歇性地,我不斷收到這個錯誤。該應用程序有需要時啓動,並在進入爲我提供登錄登錄(我100%肯定它是正確的),給出以下錯誤:請求的操作需要一個OLE DB會話對象 - VB6到SQL Server 2000

運行時錯誤「3709」
請求操作需要OLE DB Session 對象,目前的提供者不支持該對象。

真是奇怪的是,昨晚我能夠登錄,絕對沒有問題。但是,在大約一週之前,我遇到了這個問題,但是我已經出門幾天了,當我回來時我可以再次登錄。在初始實例之前,我能夠正常登錄。我注意到已經發布了一個類似的問題,但是給出的解決方案並不適合我。這是與建立數據庫連接有關的代碼。請注意,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工作,我有點不知所措。我無法弄清楚它爲什麼有時會起作用,而不是其他人。如果有人有任何見解,他們將非常感激。

+0

連接聲音沒有成功打開,或者在另一行代碼中沒有提到。這個錯誤很容易被調試。 'debug.print'在產生錯誤的行之前的連接狀態。 – Martin

回答

0

更改錯誤處理以更好地瞭解發生了什麼。既然你正在設置你的連接(設置m_DBConnection =新的ADODB.Connection)到一個新的對象,你的檢查(m_DBConnection是Nothing)並沒有太大的作用,因爲ojbect肯定已經存在。