0

在我的 ASP 代码中,当使用 asp 引擎将其作为服务器端脚本运行时,我没有任何问题。但是,当我在 VBS 文件中使用 VB 脚本运行相同的连接时,它永远不会连接到数据库?我有 Windows 2008 和 2008 R2 MSSQL。有任何想法吗?

 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
4

1 回答 1

0

Application 对象仅在 ASP 下运行 VBScript 时存在。如果 .vbs 文件通过 WScript.exe 或 CScript.exe 在 Windows 脚本宿主 (WSH) 下运行,则 Application 对象不可用。(我猜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
于 2012-07-24T22:37:26.250 回答