在 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 能力有許多用途:
- 用戶端簡化。使用者不需要擔心設定 DSN、正確地命名 DSN 及設定選項等問題,所有的作業都可由應用程式動態完成。
- 解決許多 JET 引擎連線及連接字串快取的問題。
- 增加應用程式的彈性。
在作業中建立並刪除 DSN 可在 16 位元 ODBC 中實現所有這些用途。這個方法對簡單的 DSN
管理來說也很有用,可使用程式碼來隨時自動建立、修改或刪除 DSN。Visual Basic 提供了使用 DBEngine.RegisterDatabase()
方法來建立 DSN 的能力,但是 API 則提供了更好的功能及能力來修改及移除 DSN。
逐步範例
- 開啟新的專案。
- 在 [工具] 功能表下 [選項] 對話方塊中的 [進階] 索引標籤上,將名為 WIN 32 的條件式編譯的引數設定為
1 (如果使用的是 Visual Basic 4.0 32 位元),或 0 (如果使用的是 Visual Basic 4.0 16
位元)。
- 將兩個 CommandButton 加入預設表單。
- 將下列程式碼加入至一般宣告:
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
- 將下列程式碼加入 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
- 將下列程式碼加入 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
- 執行專案。
- 按一下 Command1,加入名為 DSN_TEMP 的 DSN。
- 按一下 Command2,移除名為 DSN_TEMP 的 DSN。
ODBC 2.0 程式設計人員參考資料及 SDK 手冊 (英文)
Microsoft Press
1993
ISBN 1-55615-658-8
(c) Microsoft Corporation
1997。版權所有。本文內容由 Microsoft Corporation,Troy Cambra 提供