您可以使用 DNS-LESS 方法和应用程序的配置文件来实现此目的。
逻辑:
- 您的应用程序有一个 AutoExe 宏,该宏会在应用程序启动时触发并检查其开发环境还是生产环境并更新所有链接的表/查询。
- 在 AutoExe 中读取配置文件并检索服务器详细信息。 (您的开发文件夹将包含一个包含开发服务器详细信息的配置文件,您的发布包将包含生产服务器详细信息)
- 最好生成一个 FN_GET_CONNECTION_STRING 函数,该函数将读取您的配置文件并为您构建连接字符串
- 遍历所有链接表并使用新的服务器详细信息更新“连接”连接字符串。
- 仅在应用程序第一次运行时更新链接表,并在配置文件或本地表中记住这一点。任何进一步的运行都不需要更新服务器详细信息。
- 为您的用户提供手动重新同步表的方法
一些步骤:
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.
祝你好运.. :)