簡體   English   中英

更改無DSN訪問前端的SQL連接信息

[英]Changing SQL connection information for DSN-less Access frontend

我有一個任務關鍵型Access 2003數據庫,它使用Microsoft SQL Server數據庫遷移助手(SSMA)軟件從本地MDB更改為使用MS SQL Server 2005后端的MDB前端。

現在,我需要永久地將表所鏈接的服務器從IP地址 (即將更改)更改指向同一服務器的主機名 服務器本身不會改變,只是連接字符串。

它是無DSN連接,因此ODBC信息包含在Access MDB文件中。 如果我嘗試刷新Access中的表鏈接,它會提示我輸入DSN(我不想使用它)。

我做了一些谷歌搜索,我發現了幾個代碼片段,每次程序啟動時都會自動更新。 但是,我擔心這可能會給用戶帶來問題或延遲。 這是我最好的選擇,還是有一些技巧可以永久更改存儲在MDB中的連接字符串?

您可以使用VBA更改鏈接的TableDef.Connect屬性。

從立即窗口中查看此示例。 (我使用Replace()只是為了分割那條長線。)

? Replace(CurrentDb.TableDefs("remote_table").Connect, ";", ";" & vbCrLf)
ODBC;
DRIVER=SQL Server Native Client 10.0;
SERVER=HP64\SQLEXPRESS;
Trusted_Connection=Yes;
APP=Microsoft Office 2003;
WSID=WIN732B;
DATABASE=testbed;

所以我可以使用不同的SERVER構建一個新字符串,並將新字符串分配給TableDef .Connect屬性。

如果這是一個永久性更改,您只需要執行一次,而不是每次打開數據庫時都這樣做。

當我完成類似的連接更改時,它位於不同的服務器之間。 所以我刪除了TableDef並重新創建它,以確保Access沒有保留任何有關該連接的緩存元信息,這些信息現在已經過時了。 但是,在您的情況下,您正在處理相同的物理服務器,只是通過名稱而不是IP來引用它。 我懷疑緩存的信息對你來說是一個問題。

以下代碼多年來一直很好用:

Function LinkTable(DbName As String, SrcTblName As String, _
                   Optional TblName As String = "", _
                   Optional ServerName As String = DEFAULT_SERVER_NAME, _
                   Optional DbFormat As String = "ODBC") As Boolean
Dim db As dao.Database
Dim TName As String, td As TableDef

    On Error GoTo Err_LinkTable

    If Len(TblName) = 0 Then
        TName = SrcTblName
    Else
        TName = TblName
    End If

    'Do not overwrite local tables.'
    If DCount("*", "msysObjects", "Type=1 AND Name=" & Qt(TName)) > 0 Then
        MsgBox "There is already a local table named " & TName
        Exit Function
    End If

    Set db = CurrentDb
    'Drop any linked tables with this name'
    If DCount("*", "msysObjects", "Type In (4,6,8) AND Name=" & Qt(TName)) > 0 Then
        db.TableDefs.Delete TName
    End If

    With db
        Set td = .CreateTableDef(TName)
        td.Connect = BuildConnectString(DbFormat, ServerName, DbName)
        td.SourceTableName = SrcTblName
        .TableDefs.Append td
        .TableDefs.Refresh
        LinkTable = True
    End With

Exit_LinkTable:
    Exit Function
Err_LinkTable:
    'Replace following line with call to error logging function'
    MsgBox Err.Description
    Resume Exit_LinkTable
End Function



Private Function BuildConnectString(DbFormat As String, _
                                    ServerName As String, _
                                    DbName As String, _
                                    Optional SQLServerLogin As String = "", _
                                    Optional SQLServerPassword As String = "") As String
    Select Case DbFormat
    Case "NativeClient10"
        BuildConnectString = "ODBC;" & _
                             "Driver={SQL Server Native Client 10.0};" & _
                             "Server=" & ServerName & ";" & _
                             "Database=" & DbName & ";"
        If Len(SQLServerLogin) > 0 Then
            BuildConnectString = BuildConnectString & _
                                 "Uid=" & SQLServerLogin & ";" & _
                                 "Pwd=" & SQLServerPassword & ";"
        Else
            BuildConnectString = BuildConnectString & _
                                 "Trusted_Connection=Yes;"
        End If

    Case "ADO"
        If Len(ServerName) = 0 Then
            BuildConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                 "Data Source=" & DbName & ";"
        Else
            BuildConnectString = "Provider=sqloledb;" & _
                                 "Server=" & ServerName & ";" & _
                                 "Database=" & DbName & ";"
            If Len(SQLServerLogin) > 0 Then
                BuildConnectString = BuildConnectString & _
                                     "UserID=" & SQLServerLogin & ";" & _
                                     "Password=" & SQLServerPassword & ";"
            Else
                BuildConnectString = BuildConnectString & _
                                     "Integrated Security=SSPI;"
            End If
        End If
    Case "ODBC"
        BuildConnectString = "ODBC;" & _
                             "Driver={SQL Server};" & _
                             "Server=" & ServerName & ";" & _
                             "Database=" & DbName & ";"
        If Len(SQLServerLogin) > 0 Then
            BuildConnectString = BuildConnectString & _
                                 "Uid=" & SQLServerLogin & ";" & _
                                 "Pwd=" & SQLServerPassword & ";"
        Else
            BuildConnectString = BuildConnectString & _
                                 "Trusted_Connection=Yes;"
        End If
    Case "MDB"
        BuildConnectString = ";Database=" & DbName
    End Select
End Function


Function Qt(Text As Variant) As String
Const QtMark As String = """"
    If IsNull(Text) Or IsEmpty(Text) Then
        Qt = "Null"
    Else
        Qt = QtMark & Replace(Text, QtMark, """""") & QtMark
    End If
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM