2012-07-24 84 views
0

在我的ASP代碼中,我使用asp引擎將其作爲服務器端腳本運行時沒有任何問題。但是,當我用VB腳本在VBS文件中運行這個相同的連接時,它永遠不會連接到數據庫?我有Windows 2008和2008 R2 MSSQL。有任何想法嗎?VBScript WScript Db連接

establish connection 
function DatabaseConnection() 

' establish connection if not connected already 
if not IsObject(objGlobalConn) then 
    if Len(Application("WebConnectionString")) = 0 then 
     Set oShell = CreateObject("WScript.Shell") 
     Application.Lock 
     Application("WebConnectionString") = oShell.RegRead("HKLM\SOFTWARE\TB\ConnectionString3") 
     Application.Unlock 
    end if 
    set objGlobalConn = CreateObject("ADODB.Connection") 
    objGlobalConn.ConnectionTimeout = 0 
    objGlobalConn.CommandTimeOut = 0 
    objGlobalConn.CursorLocation = 3 ' adUseClient 
    objGlobalConn.Open Application("WebConnectionString") 
end if 

' return connection object 
set DatabaseConnection = objGlobalConn 

end function 

我的VBScript文件:

' get the connection string 
Set oShell = CreateObject("WScript.Shell") 
sConnectionString = oShell.RegRead("HKLM\SOFTWARE\TB\ConnectionString3") 
Set oShell = Nothing 

回答

0

的應用對象只ASP下運行的VBScript時存在。如果通過WScript.exe或CScript.exe在Windows腳本宿主(WSH)下運行.vbs文件,則應用程序對象不可用。 (我猜這裏有一個On Error Resume Next聲明,它抑制了WSH正在提升的錯誤信息。)

根據您是否真的需要將連接字符串存儲在Application對象中,您會想到兩種解決方案。假設Application("WebConnectionString")不在DatabaseConnection函數之外使用,則可以刪除對Application的引用。然後,該功能可以在ASP和WSH運行:

function DatabaseConnection() 
    Dim objGlobalConn, oShell, connectionString 

    ' establish connection if not connected already 
    if not IsObject(objGlobalConn) then 
     Set oShell = CreateObject("WScript.Shell") 
     connectionString = oShell.RegRead("HKLM\SOFTWARE\TB\ConnectionString3") 
     Set oShell = Nothing 

     set objGlobalConn = CreateObject("ADODB.Connection") 
     objGlobalConn.ConnectionTimeout = 0 
     objGlobalConn.CommandTimeOut = 0 
     objGlobalConn.CursorLocation = 3 ' adUseClient 
     objGlobalConn.Open connectionString 
    end if 

    ' return connection object 
    set DatabaseConnection = objGlobalConn 

end function 

但是,如果Application("WebConnectionString")使用DatabaseConnection功能之外,還需要更多一點的代碼來獲得ASP和WSH下工作的功能。引用該Application對象,或任何其他ASP相關對象的代碼的其他部分,將需要用電話進行類似防護,以IsRunningUnderASP

function DatabaseConnection() 
    Dim objGlobalConn, oShell, connectionString 

    ' establish connection if not connected already 
    if not IsObject(objGlobalConn) then 
     Set oShell = CreateObject("WScript.Shell") 
     connectionString = oShell.RegRead("HKLM\SOFTWARE\TB\ConnectionString3") 
     Set oShell = Nothing 

     If IsRunningUnderASP() And (Len(Application("WebConnectionString")) = 0) then 
      Application.Lock 
      Application("WebConnectionString") = connectionString 
      Application.Unlock 
     End If 

     set objGlobalConn = CreateObject("ADODB.Connection") 
     objGlobalConn.ConnectionTimeout = 0 
     objGlobalConn.CommandTimeOut = 0 
     objGlobalConn.CursorLocation = 3 ' adUseClient 
     objGlobalConn.Open connectionString 
    end if 

    ' return connection object 
    set DatabaseConnection = objGlobalConn 

end function 

Function IsRunningUnderASP 
    IsRunningUnderASP = _ 
    IsObject(Application) And _ 
    IsObject(Request) And _ 
    IsObject(Response) 
End Function 
+0

謝謝,我不知道爲什麼這行之有效於2003年,而不是2008年? Windows明顯改變了這些連接類型的設置。 – 2012-07-25 22:54:12

+0

只是fyi,你的腳本不能在VBS中工作,這是我需要它的地方。我猜測還有比簡單的代碼更改更多。 – 2012-07-25 23:51:48

+0

哪部分不工作? – 2012-07-26 02:24:26