【问题标题】:MS Access switching connection between test server and production SQL Server测试服务器和生产 SQL Server 之间的 MS Access 切换连接
【发布时间】:2016-02-10 06:56:09
【问题描述】:

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

假设我在连接到测试服务器的同时对前端 Access 应用程序进行了一些开发,而在部署时,我想将该前端应用程序连接到生产服务器。如何更改连接?我需要重新链接所有表格吗?

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

【问题讨论】:

  • 您的连接是否使用 DSN?
  • 目前我只有几个用户,我可以在每个工作站上设置 DSN,所以是的,他们使用 DSN。我并没有过多地研究它,但将来我可能会考虑切换到无 DSN(如果可能的话)并且如果值得的话。 (我假设 DSN-less 通常需要更多的设置工作。)

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


【解决方案1】:

您可以使用 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=

我建立了如下连接字符串:

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.

祝你好运.. :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2016-01-04
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多