1

我继承了一个使用 SQL Server 2008 后端的 Access 2007 ADP 应用程序。是否可以从命令行或某些 VBScript 更改应用程序使用的服务器连接?目前,当我将应用程序发布到测试/UAT/生产环境时,我必须打开项目,更改服务器连接,然后再次保存。

我正在尝试尽可能地自动化构建过程,目前这是最后剩下的手动任务之一。

4

1 回答 1

1

嗯,我的 Google-fu 很弱。我刚刚在 MSDN 上找到了一篇文章,它提供了 VBA 脚本来实现这一点。我修改它以作为下面的 VBScript 运行:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

If (WScript.Arguments.Count = 0) Then
    WScript.Echo "ERROR - the Access project name must be specified"
    WScript.Quit()
End If

Dim sAccessProject
sAccessProject = fso.GetAbsolutePathName(WScript.Arguments(0))

If (fso.FileExists(sAccessProject) = False) Then
    WScript.Echo "ERROR - the Access project could not be located : " & sAccessProject
    WScript.Quit()
End If

If (WScript.Arguments.Count = 1) Then
    WScript.Echo "ERROR - the SQL Server hostname must be specified"
    WScript.Quit()
End If

Dim sServer
sServer = WScript.Arguments(1)

If (WScript.Arguments.Count = 2) Then
    WScript.Echo "ERROR - the SQL Server database name must be specified"
    WScript.Quit()
End If

Dim sDatabase
sDatabase = WScript.Arguments(2)

Dim sUsername
If (WScript.Arguments.Count = 3) Then
    sUsername = ""
Else
    sUsername = WScript.Arguments(3)
End If

Dim sPassword
If (WScript.Arguments.Count >= 3) Then
    sPassword = ""
Else
    sPassword = WScript.Arguments(4)
End If

ChangeADPConnection sAccessProject, sServer, sDatabase, sUsername, sPassword

Function ChangeADPConnection(strProjectName, strServerName, strDBName, strUN , strPW)

    Dim strConnect

    Dim oApplication
    Set oApplication = CreateObject("Access.Application")
    WScript.Echo "Starting MS Access"
    WScript.Echo "Opening " & strProjectName & " ..."
    oApplication.OpenAccessProject strProjectName
    oApplication.Visible = false

    oApplication.CurrentProject.CloseConnection

    'The Provider, Data Source, and Initial Catalog arguments are required.
    strConnect = "Provider=SQLOLEDB.1" & _
    ";Data Source=" & strServerName & _
    ";Initial Catalog=" & strDBName
    If strUN <> "" Then
        strConnect = strConnect & ";User ID=" & strUN
        If strPW <> "" Then
            strConnect = strConnect & ";Password=" & strPW
        End If
    Else  'Try to use integrated security if no username is supplied.
        strConnect = strConnect & ";Integrated Security=SSPI"
    End If

    WScript.Echo "Setting connection string to " & strConnect

    oApplication.CurrentProject.OpenConnection strConnect
    oApplication.Quit()
    Set oApplication = Nothing

End Function

要运行它,只需从命令行使用以下命令:

cscript connect.vbs Project.adp, "ServerName", "DatabaseName", "Username", "Password"
于 2013-05-09T01:05:56.373 回答