ms-access 中与 mysql 服务器的 DSN-less 连接不记得用户名和密码
为了便于分发,我希望所有 mysql 链接表都是无 DSN 的。我还希望能够轻松地将表重新链接到不同的服务器(出于测试目的),因此我在 vba 代码中进行链接设置。这是我用来设置/刷新任何链接的代码:
Private Sub relink_mysql_tables(mysql_connection As String)
Dim db As Database
Set db = CurrentDb()
Dim tbldef As TableDef
For Each tbldef In db.TableDefs
If (tbldef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
tbldef.Connect = mysql_connection
tbldef.RefreshLink
End If
Next
End Sub
我使用的 mysql_connection 字符串是:
DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3
这一切都很好,一切正常,直到我关闭 ms-access 并重新打开它。用户名和密码似乎忘记了。例如,如果我尝试打开链接表,则会出现一个 ODBC 连接器弹出窗口,要求我输入用户名和密码。
我注意到,运行上述重新链接代码后,如果我将鼠标指针悬停在链接表(在本例中为“tender”表)上,它会显示当前连接字符串,但省略用户名和密码:
ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Option=3;TABLE=tender
我在其他地方读到将“;Persist Security Info=True”添加到连接字符串会起作用,但事实并非如此!下次重新启动ms-access时,用户名和密码仍然忘记。我当前的解决方案是每次启动数据库时重新运行重新链接代码,但这对我来说似乎是不必要的开销。访问是否可以记住链接表中的用户名和密码?
顺便说一句,我使用的是Mysql5.5和Access2007。
For ease of distribution I want all the mysql linked tables to be DSN-less. I also want to be able to relink the tables to a different server easily (for test purposes), so I'm doing the link setup in vba code. This is the code I use to setup/refresh any links:
Private Sub relink_mysql_tables(mysql_connection As String)
Dim db As Database
Set db = CurrentDb()
Dim tbldef As TableDef
For Each tbldef In db.TableDefs
If (tbldef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
tbldef.Connect = mysql_connection
tbldef.RefreshLink
End If
Next
End Sub
The mysql_connection string I use is:
DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3
This is all fine and dandy and everything works until I close ms-access and reopen it. The user name and password seems to get forgotten. If I try and open a linked table, for example, I'm presented with a ODBC Connector pop-up asking me to enter user name and password.
I notice that after running the above relink code that if I hover the mouse pointer over a linked table (a table called 'tender' in this instance) it shows the current connection string, but with the user name and password omitted:
ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Option=3;TABLE=tender
I read elsewhere that adding ";Persist Security Info=True" to the connection string would work but it doesn't! The user name and password is still forgotten the next time I restart ms-access. My current solution is to re-run the relink code every-time the database is started but this seems like an unnecessary overhead to me. Is the a way for access to remember the user name and password within the linked tables?
I'm using Mysql5.5 and Access2007 by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这篇微软文章的帮助下找到了答案。不要使用 RefreshLink 方法,而是使用 dbAttachedODBC 选项删除并重新创建链接:
连接字符串与以前相同,但添加了前缀“ODBC;”:
I found an answer with the help of this microsoft article. Rather than using the RefreshLink method, delete and recreate the link with the dbAttachedODBC option:
The connection string is the same as before but with the addition of the prefix "ODBC;":