2013-05-07 35 views
1

我繼承了使用SQL Server 2008後端的Access 2007 ADP應用程序。是否可以通過命令行或某些VBScript更改應用程序使用的服務器連接?目前,當我將應用程序發佈到測試/ UAT /生產環境時,我必須打開項目,更改服務器連接並再次保存。從命令行更改Access服務器連接

我試圖儘可能自動化構建過程,目前這是最後剩下的手動任務之一。

回答

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" 
+0

我喜歡這個劇本的偉大工程,但運行時不需要逗號它 – 2016-06-22 10:56:05

相關問題