测试服务器和生产SQL Server之间的MS Access切换连接

时间:2016-02-10 06:56:10

标签: sql-server ms-access azure azure-sql-database ms-access-2013

我正在开发一个带有Azure SQL数据库后端的新MS Access 2013桌面应用程序。我打算设置两个后端服务器,一个用于生产,另一个用于测试。

假设我在连接到测试服务器时在前端Access应用程序上进行了一些开发,并且在部署时我想将该前端应用程序连接到生产服务器。如何更改连接?我是否必须重新链接所有表格?

是否有更好的工作流程在测试服务器上进行开发,然后部署连接到生产服务器的应用程序?

1 个答案:

答案 0 :(得分:3)

您可以使用DNS-LESS方法和应用程序的配置文件来实现此目的。

逻辑:

  1. 您的应用程序有一个AutoExe宏,它会在应用程序启动时触发,并检查其开发或生产环境并更新所有链接的表/查询。
  2. 在您的AutoExe中,您阅读配置文件并检索服务器详细信息。 (您的开发文件夹将包含一个包含开发服务器详细信息的配置文件,您的发布包将包含生产服务器详细信息)
  3. 理想情况下生成一个FN_GET_CONNECTION_STRING函数,该函数将读取您的配置文件并为您构建连接字符串
  4. 遍历所有链接表并更新" connect"连接字符串与新服务器详细信息。
  5. 仅在应用程序第一次运行时更新链接表,并在配置文件或本地表中记住这一点。任何进一步的运行都不需要更新服务器详细信息。
  6. 为您的用户提供手动重新同步表的方法
  7. 一些步骤:

    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.
    
    祝你好运.. :)