文章編號: 184608 - 上次校閱: 2003年8月19日 - 版次: 1.0

HOWTO:利用 VB 透過程式建立 SQL Server 的 DSN

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

在此頁中

全部展開 | 全部摺疊

結論

本文件示範,如何使用 Visual Basic,透過程式來建立 SQL Server 的資料來源名稱 (DSN)。本文件中所描述的技巧是, 使用 Windows 的 應用程式介面 (API) 函數來建立及操控 Windows 系統登錄中的項目。

其他相關資訊

DSN 通常是透過 [ODBC 資料來源管理員] 視窗建立的, 該視窗可以從 Windows [控制台] (或是 Windows 2000 中的 [管理員工具] ) 存取使用。 其他可存取 ODBC 式資料庫的技巧包括,使用 RegisterDatabase (資料存取物件 (DAO) 方法)、使用 SQLConfigDataSource ODBC API 函數、 或是使用無 DSN 連線字串。

但是,您也可以在 Windows 系統登錄中,手動建立及操控設定值, 來建立新的 DSN。下列技巧是使用 RegCreateKey、RegSetValueEx、 和 RegCloseKey API 函數來建立 SQL Server 資料庫的系統 DSN。

逐步進行的程序



  1. 開啟新的 Visual Basic 專案,按預設建立 Form1。 在 Form1 (Command1) 上建立 CommandButton, 再將下列程式碼放入 Form1 的 [General Declarations] 區段中:
        Option Explicit
    
        Private Const REG_SZ = 1    'Constant for a string variable type.
        Private Const HKEY_LOCAL_MACHINE = &H80000002
    
        Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
           "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
           phkResult As Long) As Long
    
        Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _
           "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
           ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _
           cbData As Long) As Long
    
        Private Declare Function RegCloseKey Lib "advapi32.dll" _
           (ByVal hKey As Long) As Long
  2. 將下列程式碼放入 Form1 上 Command1 按鈕的 click 事件中:

    視您的環境需要,變更 DataSourceName、DatabaseName、Description、 DriverPath、LastUser 和 Server 變數的值。列在 [ODBC 資料來源管理員] 視窗的 [ODBC 驅動程式] 索引標籤中之任何磁碟機都可作為 DriverPath 的一部份。這些驅動程式都可以在 Windows 95 或 Windows 98 電腦上的 C:\Windows\System 以及 Windows NT 的 C:\Winnt\System32 中找到。
       Private Sub Command1_Click()
    
       Dim DataSourceName As String
       Dim DatabaseName As String
       Dim Description As String
       Dim DriverPath As String
       Dim DriverName As String
       Dim LastUser As String
       Dim Regional As String
       Dim Server As String
    
       Dim lResult As Long
       Dim hKeyHandle As Long
    
       'Specify the DSN parameters.
    
       DataSourceName = "<the name of your new DSN>"
       DatabaseName = "<name of the server to be accessed by the new DSN>"
       Description = "<a description of the new DSN>"
       DriverPath = "<path to your SQL Server driver>"
       LastUser = "<default user ID of the new DSN>"
       Server = "<name of the server to be accessed by the new DSN>"
       DriverName = "SQL Server"
    
       'Create the new DSN key.
    
       lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _
            DataSourceName, hKeyHandle)
    
       'Set the values of the new DSN key.
    
       lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _
          ByVal DatabaseName, Len(DatabaseName))
       lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _
          ByVal Description, Len(Description))
       lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _
          ByVal DriverPath, Len(DriverPath))
       lResult = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, _
          ByVal LastUser, Len(LastUser))
       lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _
          ByVal Server, Len(Server))
    
       'Close the new DSN key.
    
       lResult = RegCloseKey(hKeyHandle)
    
       'Open ODBC Data Sources key to list the new DSN in the ODBC Manager.
       'Specify the new value.
       'Close the key.
    
       lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _
          "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
       lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _
          ByVal DriverName, Len(DriverName))
       lResult = RegCloseKey(hKeyHandle)
    
       End Sub
  3. 執行專案,然後按一下 Command1 命令按鈕,再從控制台 (或是從 Windows 2000 中的 [管理員工具]),開啟 ODBC 資料來源管理員。您的新 DSN 會與已建立的其他系統 DSN 同時顯示。

?考

如需其他相關資訊,請參閱 Microsoft Knowledge Base 中的下列文件:
166392? (http://support.microsoft.com/kb/166392/EN-US/ ) HOWTO: Use "DSN-Less" ODBC Connections with RDO
147875? (http://support.microsoft.com/kb/147875/EN-US/ ) HOWTO: Use "DSN-Less" ODBC Connections with RDO and DAO
171146? (http://support.microsoft.com/kb/171146/EN-US/ ) HOWTO: Create and Remove a DSN in Visual Basic
123008? (http://support.microsoft.com/kb/123008/EN-US/ ) HOWTO: Set Up ODBC Data Sources When Distributing Apps

?考

本文件是根據 Microsoft Knowledgebase 文件編號 Q184608 翻譯的. 若要參考原始英文文件內容, 請至以下網址:

http://support.microsoft.com/support/kb/articles/Q184/6/08.asp (http://support.microsoft.com/kb/184608/en-us?ln=en-us&sd=gn&fr=0)

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