1

我正在開發一個帶有Azure SQL數據庫後端的新MS Access 2013桌面應用程序。我計劃建立兩個後端服務器,一個用於生產,一個用於測試。測試服務器和生產之間的MS Access切換連接SQL Server

假設我在連接到測試服務器的前端Access應用程序上進行了一些開發,並且在部署時我想將該前端應用程序連接到生產服務器。如何更改連接?我需要重新鏈接所有表格嗎?

是否有更好的工作流程用於在測試服務器上進行開發,然後部署連接到生產服務器的應用程序?

+0

做你的連接使用DSN? – HansUp

+0

目前我只有幾個用戶,我可以在每個工作站上設置DSN,所以是的,他們使用DSN。我沒有太多注意,但是在將來我可能會考慮切換到DSN-less(如果可能的話),如果值得的話。 (我假設DSN-less通常需要更多的工作來設置。) – fortuneRice

回答

2

您可以使用DNS-LESS方法和應用程序的配置文件來實現此目的。

邏輯:

  1. 你的應用程序有一個AutoExe宏觀它將觸發在應用程序啓動和檢查自己的開發或生產環境,並更新所有鏈接的表/查詢。
  2. 在您的AutoExe中,您讀取配置文件並檢索服務器詳細信息。 (你的開發文件夾將與開發服務器詳細信息的配置文件,您的發佈包將包含生產服務器的詳細信息)
  3. 理想的情況下產生FN_GET_CONNECTION_STRING功能將通過所有的讀取您的配置文件,並建立連接字符串爲您
  4. 循環鏈接表並使用新的服務器詳細信息更新「連接」連接字符串。
  5. 只有在應用程序第一次運行時更新鏈接表,並在配置文件或本地表中記住這一點。任何進一步的運行都不需要更新服務器的詳細信息。
  6. 您的用戶提供一種方式來手動重新同步的表

一些步驟:

Private Function FN_REFRESH_CONNECTIONS(Optional iForce As Boolean = False) As Boolean 
'read through all linked tabled and update the connectionstring, if force is set as true, update the connection string and connect to the actual server(refreshlink) 
For Each tdf In db.TableDefs 
     If tdf.connect <> vbNullString Then 
       If Not FN_CONNECT_TABLE(tdf, iForce) Then 
        Err.Raise 1, Err.Source, "Driver missing error " & Err.description 
       End If 
       myCurrCount = myCurrCount + 1 
       lbl_count.caption = myCurrCount & " of " & myCount & "- Done : please wait hard linking is in progress" 
       DoEvents 
      End If 
    Next tdf 
End Function 


Public Function FN_CONNECT_TABLE(ByRef iTdf As dao.TableDef, iConnect As Boolean) As Boolean 
    ' This function takes tablename and connects to the backend server 
    FN_CONNECT_TABLE = False 
    On Error GoTo Final_Error: 
    iTdf.connect = GET_CONNECTION_STRING & "TABLE=" & iTdf.name 
    If iConnect Then iTdf.RefreshLink 
    FN_CONNECT_TABLE = True 

    Exit Function 

Final_Error: 
    FN_CONNECT_TABLE = False 
End Function 

獲取連接字符串函數是你讀你的配置文件,建立自己的連接字符串。我有點長,因爲我有SSL和不同的服務器配置文件,但這裏有一些代碼是如何創建和讀取config.ini的。

Option Compare Database 
Option Explicit 

Declare Function GetPrivateProfileString Lib "kernel32" Alias _ 
    "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ 
    ByVal lpKeyName As Any, ByVal lpDefault As String, _ 
    ByVal lpReturnedString As String, ByVal nSize As Long, _ 
    ByVal lpFileName As String) As Long 

Declare Function WritePrivateProfileString Lib "kernel32" _ 
    Alias "WritePrivateProfileStringA" _ 
    (ByVal sSectionName As String, _ 
    ByVal sKeyName As String, _ 
    ByVal sString As String, _ 
    ByVal sFileName As String) As Long 

Private Const mINI_PATH = "Config.ini" 
'Read more at http://vbadud.blogspot.com/2008/11/how-to-read-and-write-configuration.html#mQciBJHBBX5cE52E.99 

Public Function FN_GET_INI_VALUE(ByVal strSectionName As String, ByVal strEntry As String) As String 

    Dim X As Long 
    Dim sSection As String, sEntry As String, sDefault As String 
    Dim sRetBuf As String, iLenBuf As Integer, sFileName As String 
    Dim sValue As String 

    On Error GoTo ErrGetSectionentry 
    sSection = strSectionName 
    sEntry = strEntry 
    sDefault = "" 
    sRetBuf = Strings.String$(256, 0) '256 null characters 
    iLenBuf = Len(sRetBuf$) 
    sFileName = FN_GET_BASE_PATH & mINI_PATH 
    X = GetPrivateProfileString(sSection, sEntry, _ 
    "", sRetBuf, iLenBuf, sFileName) 
    sValue = Strings.Trim(Strings.Left$(sRetBuf, X)) 

    If sValue <> "" Then 
     FN_GET_INI_VALUE = sValue 
    Else 
     FN_GET_INI_VALUE = vbNullChar 
    End If 

ErrGetSectionentry: 
    If Err <> 0 Then 
    Err.Clear 
    Resume Next 
    End If 

End Function 

Public Function FN_SET_INI_VALUE(iSection As String, iItem As String, iValue As String) As Boolean 
    On Error Resume Next 
    Dim ret As Variant 
    Dim mDummy As Variant 
    mDummy = FN_GET_INI_VALUE(iSection, iItem) 
    ret = WritePrivateProfileString(iSection, iItem, iValue, FN_GET_BASE_PATH & mINI_PATH) 
    FN_SET_INI_VALUE = ret > 0 
End Function 

然後我通過閱讀config.ini文件來讀取服務器的詳細信息來構建我的連接字符串。

DBNAME = Nz(FN_GET_INI_VALUE(prod-server, "MySQL-DB-Name"), "") 

.. dbserver = Nz(FN_GET_INI_VALUE(prod-server, "MySQL-SERVER"), "") 
.. dbpassword i kept this hard-coded for security purpose 

我的config.ini文件看起來是這樣的:

[prod-server] 
ip= 
ip1= 
ipv6= 
server-name= 
web-port=8081 
web-server-name= 
ftp-port=21 
ftp-use-tsl=true 
MySQL-SSL-Enabled= 
MySQL-Port=3306 
MySQL-User= 
MySQL-DB-Name= 

和我建立connection_string,如:

CON = "ODBC;DRIVER={" & Driver & "};PORT=" & mPort & ";DATABASE=" & mDatabase & ";SERVER={" & mServer & "};User={" & mUser & "};Password={" & mPassword & "};" 
'Where driver is you odbc driver or your custom driver. you can manually set the driver or get it by reading odbc driver section in the registry. 

也祝你好運.. :)

相關問題