手动关闭 ODBC DSN 连接
Manually Close ODBC DSN Connection
我有一个 MS Access 数据库,它通过用户级 DNS ODBC 数据源连接到另一个数据库。
首次启动连接时,ODBC 驱动程序会提示我输入用户名和密码。它随后连接到服务器上的数据库取决于我使用的用户名。
建立连接后,Access 将保留/保持它处于活动状态,直到我关闭 Access 数据库。
有没有办法强制 Access 关闭所有打开的 ODBC 连接,要求我在下次尝试访问服务器上的对象时再次提供登录凭据。我想要做的是切换 ODBC 连接正在访问的数据库,而不必完全关闭 Access 并重新打开数据库。
哦,链接 tables。我认为这是不可能的。
您可以尝试重置连接信息,但我怀疑它是否有效:
Dim TD As TableDef
For Each TD In DB.TableDefs
' Linked table? You can also check the .Connect string more specifically for KB_SQL
If TD.Connect <> "" Then
TD.Connect = TD.Connect
TD.RefreshLink
End If
Next TD
一种可能真正有效的更激进的方法是删除 table 链接并重新添加它们。
Dim TD As TableDef
Dim sConn As String, sName As String
For Each TD In DB.TableDefs
If TD.Connect <> "" Then
sName = TD.Name
sConn = TD.Connect
' Remove linked table
DB.TableDefs.Delete sName
' and add it as new link
DoCmd.TransferDatabase acLink, "ODBC Database", sConn, acTable, sName, sName
End If
Next TD
您可能需要与 For Each 不同类型的循环,因为您正在修改循环中的 DB.TableDefs 集合。
两件事:
1) 您可以通过以下方式让您的用户尽可能轻松地关闭和重新打开 Access:
' Close and restart
Shell "restart.bat", vbNormalFocus
Application.Quit
restart.bat 由
组成
REM wait for Access to close
TIMEOUT 3
REM your actual command line goes here
msaccess.exe mydatabase.mdb
2) 您提到:
What I want to be able to do is switch which database the ODBC
connection is accessing
你发布的link给了我一个想法:
Any subsequent ODBC objects that happen to match on three
parameters—ODBC driver, server, and database—will reuse that cached
connection.
如果您将 linked table 从用户 DSN 连接切换到无 DSN 连接,您期望的行为应该会自动发生。
只需通过更改 "Database=..." 部分来更改所有 table 的 .Connect 字符串,并且 Access 应该在下次访问 table 时询问凭据。
我有一个 MS Access 数据库,它通过用户级 DNS ODBC 数据源连接到另一个数据库。
首次启动连接时,ODBC 驱动程序会提示我输入用户名和密码。它随后连接到服务器上的数据库取决于我使用的用户名。
建立连接后,Access 将保留/保持它处于活动状态,直到我关闭 Access 数据库。
有没有办法强制 Access 关闭所有打开的 ODBC 连接,要求我在下次尝试访问服务器上的对象时再次提供登录凭据。我想要做的是切换 ODBC 连接正在访问的数据库,而不必完全关闭 Access 并重新打开数据库。
哦,链接 tables。我认为这是不可能的。
您可以尝试重置连接信息,但我怀疑它是否有效:
Dim TD As TableDef
For Each TD In DB.TableDefs
' Linked table? You can also check the .Connect string more specifically for KB_SQL
If TD.Connect <> "" Then
TD.Connect = TD.Connect
TD.RefreshLink
End If
Next TD
一种可能真正有效的更激进的方法是删除 table 链接并重新添加它们。
Dim TD As TableDef
Dim sConn As String, sName As String
For Each TD In DB.TableDefs
If TD.Connect <> "" Then
sName = TD.Name
sConn = TD.Connect
' Remove linked table
DB.TableDefs.Delete sName
' and add it as new link
DoCmd.TransferDatabase acLink, "ODBC Database", sConn, acTable, sName, sName
End If
Next TD
您可能需要与 For Each 不同类型的循环,因为您正在修改循环中的 DB.TableDefs 集合。
两件事:
1) 您可以通过以下方式让您的用户尽可能轻松地关闭和重新打开 Access:
' Close and restart
Shell "restart.bat", vbNormalFocus
Application.Quit
restart.bat 由
组成REM wait for Access to close
TIMEOUT 3
REM your actual command line goes here
msaccess.exe mydatabase.mdb
2) 您提到:
What I want to be able to do is switch which database the ODBC connection is accessing
你发布的link给了我一个想法:
Any subsequent ODBC objects that happen to match on three parameters—ODBC driver, server, and database—will reuse that cached connection.
如果您将 linked table 从用户 DSN 连接切换到无 DSN 连接,您期望的行为应该会自动发生。
只需通过更改 "Database=..." 部分来更改所有 table 的 .Connect 字符串,并且 Access 应该在下次访问 table 时询问凭据。