文章編號: 171146 - 上次校閱: 2006年8月14日 - 版次: 3.2

如何在 Visual Basic 中建立及移除 DSN

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

在 32 位元版本的 Visual Basic 4.0 中可以使用無 DSN 連線 (DSN-Less Connection),但是因為 16 位元版本的 ODBC 無法處理此語法,所以在 16 位元版本的 Visual Basic 4.0 中無法使用無 DSN 連線。您可以使用 SQLConfigDataSource ODBC API 呼叫,在作業中動態地建立及移除「資料來源名稱」(Data Source Name,DSN) 模擬此功能。

下列是示範此項技術的 16 及 32 位元範例。包含 32 位元程式碼的原因是此項技術還有其他的用途,如後所述。本文所述的 32 位元技術也適用於 Visual Basic 5.0。

其他相關資訊

使用無 DSN 連線的 32 位元 ODBC 能力有許多用途:
  1. 用戶端簡化。使用者不需要擔心設定 DSN、正確地命名 DSN 及設定選項等問題,所有的作業都可由應用程式動態完成。
  2. 解決許多 JET 引擎連線及連接字串快取的問題。
  3. 增加應用程式的彈性。
在作業中建立並刪除 DSN 可在 16 位元 ODBC 中實現所有這些用途。這個方法對簡單的 DSN 管理來說也很有用,可使用程式碼來隨時自動建立、修改或刪除 DSN。Visual Basic 提供了使用 DBEngine.RegisterDatabase() 方法來建立 DSN 的能力,但是 API 則提供了更好的功能及能力來修改及移除 DSN。

逐步範例

  1. 開啟新的專案。
  2. 在 [工具] 功能表下 [選項] 對話方塊中的 [進階] 索引標籤上,將名為 WIN 32 的條件式編譯的引數設定為 1 (如果使用的是 Visual Basic 4.0 32 位元),或 0 (如果使用的是 Visual Basic 4.0 16 位元)。
  3. 將兩個 CommandButton 加入預設表單。
  4. 將下列程式碼加入至一般宣告:
          Option Explicit
    
          'Constant Declaration
          Private Const ODBC_ADD_DSN = 1        ' Add data source
          Private Const ODBC_CONFIG_DSN = 2     ' Configure (edit) data source
          Private Const ODBC_REMOVE_DSN = 3     ' Remove data source
          Private Const vbAPINull As Long = 0&  ' NULL Pointer
    
          'Function Declare
          #If WIN32 Then
    
              Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
              (ByVal hwndParent As Long, ByVal fRequest As Long, _
              ByVal lpszDriver As String, ByVal lpszAttributes As String) _
              As Long
          #Else
              Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" _
              (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal _
              lpszDriver As String, ByVal lpszAttributes As String) As Integer
          #End If
    					
  5. 將下列程式碼加入 Command1 的 Click 事件:
          #If WIN32 Then
              Dim intRet As Long
          #Else
              Dim intRet As Integer
          #End If
          Dim strDriver As String
          Dim strAttributes As String
    
          'Set the driver to SQL Server because it is most common.
          strDriver = "SQL Server"
          'Set the attributes delimited by null.
          'See driver documentation for a complete
          'list of supported attributes.
          strAttributes = "SERVER=SomeServer" & Chr$(0)
          strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
          strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
          strAttributes = strAttributes & "DATABASE=pubs" & Chr$(0)
          'To show dialog, use Form1.Hwnd instead of vbAPINull.
          intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
          strDriver, strAttributes)
          If intRet Then
              MsgBox "DSN Created"
          Else
              MsgBox "Create Failed"
          End If
    					
  6. 將下列程式碼加入 Command2 的 Click 事件:
          #If WIN32 Then
              Dim intRet As Long
          #Else
              Dim intRet As Integer
          #End If
          Dim strDriver As String
          Dim strAttributes As String
    
          'Set the driver to SQL Server because most common.
          strDriver = "SQL Server"
          'Set the attributes delimited by null.
          'See driver documentation for a complete list of attributes.
          strAttributes = "DSN=DSN_TEMP" & Chr$(0)
          'To show dialog, use Form1.Hwnd instead of vbAPINull.
          intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
          strDriver, strAttributes)
          If intRet Then
              MsgBox "DSN Deleted"
          Else
              MsgBox "Delete Failed"
          End If
    					
  7. 執行專案。
  8. 按一下 Command1,加入名為 DSN_TEMP 的 DSN。
  9. 按一下 Command2,移除名為 DSN_TEMP 的 DSN。

?考

ODBC 2.0 程式設計人員參考資料及 SDK 手冊 (英文)
Microsoft Press 1993
ISBN 1-55615-658-8

(c) Microsoft Corporation 1997。版權所有。本文內容由 Microsoft Corporation,Troy Cambra 提供

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
關鍵字:?
kbhowto KB171146
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。