ms-access 中与 mysql 服务器的 DSN-less 连接不记得用户名和密码

发布于 2024-11-01 21:56:01 字数 1160 浏览 5 评论 0原文

为了便于分发,我希望所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2024-11-08 21:56:01

我在这篇微软文章的帮助下找到了答案。不要使用 RefreshLink 方法,而是使用 dbAttachedODBC 选项删除并重新创建链接:

Public Sub relink_mysql_tables(mysql_connection As String)

Dim db As Database
Dim tblDef As TableDef
Dim sLocalTableName As String
Dim sRemoteTableName As String    

' new collection '
Dim newTableDefs As New Collection

' current database '
Set db = CurrentDb()

' create new table defs '
For Each tblDef In db.TableDefs
    If (tblDef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
        sLocalTableName = tblDef.Name
        sRemoteTableName = tblDef.SourceTableName

        ' create new linked table def '
        Set tblDef = db.CreateTableDef(sLocalTableName, dbAttachSavePWD, sRemoteTableName, mysql_connection)
        newTableDefs.Add tblDef         

    End If
Next

' delete old table defs '
For Each tblDef In newTableDefs
    db.TableDefs.Delete tblDef.Name
Next

' add new table defs to current database '
For Each tblDef In newTableDefs
    db.TableDefs.Append tblDef
Next

连接字符串与以前相同,但添加了前缀“ODBC;”:

ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3

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:

Public Sub relink_mysql_tables(mysql_connection As String)

Dim db As Database
Dim tblDef As TableDef
Dim sLocalTableName As String
Dim sRemoteTableName As String    

' new collection '
Dim newTableDefs As New Collection

' current database '
Set db = CurrentDb()

' create new table defs '
For Each tblDef In db.TableDefs
    If (tblDef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
        sLocalTableName = tblDef.Name
        sRemoteTableName = tblDef.SourceTableName

        ' create new linked table def '
        Set tblDef = db.CreateTableDef(sLocalTableName, dbAttachSavePWD, sRemoteTableName, mysql_connection)
        newTableDefs.Add tblDef         

    End If
Next

' delete old table defs '
For Each tblDef In newTableDefs
    db.TableDefs.Delete tblDef.Name
Next

' add new table defs to current database '
For Each tblDef In newTableDefs
    db.TableDefs.Append tblDef
Next

The connection string is the same as before but with the addition of the prefix "ODBC;":

ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文