簡體   English   中英

將 UID 和 PWD 保留在無 ODBC DSN 數據庫和 DAO 緩存連接中的 ADO 連接字符串之外?

[英]Keeping UID and PWD out of an ADO connection string in an ODBC DSN-less Database and a DAO cached connection?

我使用了 Ben Clothier 來自他的 Office 博客 Power Tip ( http://blogs.office.com/2011/04/08/power-tip-improve-the-security-of-database-connections/ ) 的建議來創建 DSN - 與緩存憑據的連接較少,因此在 Access 界面中工作時,不會保存或多次需要用戶的 UID 和 PWD。 有其他人這樣做過嗎? 如果是這樣,當您需要使用 ADO 連接而不是 DOA 通過 VBA 從 Access 訪問 SQL 時,您的方法是什么? 如何打開 adodb 連接而不必再次提供用戶 ID 和密碼,或者不必將其放入代碼中? (我使用的是 Access 2013 前端、SQL 2008 R2 后端、SQL Server 安全性)在此先感謝!

我的緩存連接代碼是這樣工作的:

Public Function InitConnect(strUserName As String, strPassword As String) As Boolean
' Description:  Is called in the application’s startup 
'               to ensure that Access has a cached connection
'               for all other ODBC objects’ use.

    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Dim strConnection As String


    strConnection = "ODBC;DRIVER=sql server;" & _
             "SERVER=******;" & _
             "APP=Microsoft Office 2010;" & _
             "DATABASE=******;" & _
             "Network=DBMSSOCN;"

    Set dbs = DBEngine(0)(0)
    Set qdf = dbs.CreateQueryDef("")
    With qdf
        .Connect = strConnection & _
             "UID=" & strUserName & ";" & _
             "PWD=" & strPassword & ";"
        .SQL = "Select Current_User;"

        Set rst = qdf.OpenRecordset(dbOpenSnapshot, dbSQLPassThrough)

   End With
    InitConnect = True

ExitProcedure:
    On Error Resume Next
        Set rst = Nothing
        Set qdf = Nothing
        Set dbs = Nothing
    Exit Function
End Function

然后當我需要訪問數據時,我可以這樣做(注意不需要 UID 和 PWD):

Dim dbs As DAO.Database 
Set dbs = OpenDatabase("", False, False, "ODBC;DRIVER=sql server;SERVER=*****;APP=Microsoft Office 2010;DATABASE=*****;Network=DBMSSOCN")

我還可以在 Access 或 VBA 中將 ODBC 連接設置為傳遞查詢。 但是這些連接僅在連接字符串與我的緩存連接代碼中最初使用的字符串相同時才有效。 因此,當我需要 ADODB 連接時(似乎有時需要 ADO?),字符串顯然不會完全相同。
例如:

 Dim cn As New ADODB.Connection
 cn.Open "Provider = sqloledb;Data Source=*same as "SERVER"*;Initial Catalog=*same as "DATABASE"*;User Id=****;Password=****"

這種類型的連接只有在我提供用戶 ID 和密碼時才有效。 我該如何編寫它以便我不需要它們? ~謝謝!

盡管 ACCESS 在安全方面存在一些弱點,但您可以采取一些措施將風險降至最低。 其中之一是將 DB 編譯為 ACCDE。 這樣 VBA 被編譯並且不可見。

您可以創建一個返回字符串的公共函數

Public Function GET_CONNECTION_STRING() as STRING
' construct your connection string here with server name and password
    GET_CONNECTION_STRING = "DRIVER={" & Driver & "};PORT=" & mPort & ";DATABASE=" & mDatabase & ";SERVER={" & mServer & "};UID=" & mUser & ";PWD={" & mPassword & "};"
End Function

然后創建一個在應用程序打開時運行的 AutoExe 宏。 在您的 AutoExe 中執行指向鏈接表的刷新鏈接。 類似於你所擁有的東西。

For Each tdf In db.TableDefs
   If tdf.connect <> vbNullString Then
       tdf.connect = GET_CONNECTION_STRING & ";TABLE=" & tdf.name
       tdf.RefreshLink
    End If
Next tdf

您可以對現有的傳遞查詢執行相同的操作:

For Each myQuerydef In MyDB.QueryDefs
    If Left(myQuerydef.connect, 4) = "ODBC" Then
        myQuerydef.connect = "ODBC;" & GET_CONNECTION_STRING
        myQuerydef.Close
    End If
   Next

此外,您還可以使用其他一些公共功能來獲取當前登錄的用戶名。 就像是

public function getCrruserID() as int
   'check your public variable crr_user_id if its empty redirect to login
   if nz(crr_user_id,0) = 0 then
     'go to login and save the user id after successful login
   else
      getCrruserID = crr_user_id
   end if
end function

使用簡單的 DAO 來執行 sql 代碼,如

dim db as DAO.Database
set db = currentdb
dim rs as Dao.Recordset
set rs = db.openrecordset("select something from your linked table")

或者

db.execute "update command", dbfailonerror

你的最后一個問題。 如果你在內存中保存了一些東西,一旦你的應用程序關閉,它就會被銷毀。

編輯:如果您有 50 個以上的鏈接表,則在每次啟動時刷新它們可能不是一個好主意。 相反,您可以根據需要創建一個包含 [local_Appversion, isFreshInstall] 和一些其他變量的本地表。 每次您的用戶收到更新時,freshInstall 將為 true 並編碼您的應用程序以連接和刷新所有表。 (只是為了確保客戶端將獲得不間斷的連接)

所以在你的 autoExe 代碼中:如果它是 freshInstall 然后連接和 refreshlinks 如果不只是設置 connectionString。 (通常是登錄后的啟動畫面以執行此操作)成功連接后只需將本地 isFreshInstall 值更新為 false 以便下次更快啟動。

您還可以擁有一個專用菜單,用戶可以在其中手動單擊和刷新鏈接。(以防連接斷開)類似用於訪問的自定義菜單

如果您的組織有一個域,您可以使用 Windows 登錄名允許可信連接祝您好運。

暫無
暫無

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

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