hwndParent
[Input]
Parent window handle. The function will not display any dialog boxes if the handle is null.
fRequest
[Input]
Type of request. The fRequest argument must contain one of the following values:
ODBC_ADD_DSN: Add a new user data source.
ODBC_CONFIG_DSN: Configure (modify) an existing user data source.
ODBC_REMOVE_DSN: Remove an existing user data source.
ODBC_ADD_SYS_DSN: Add a new system data source.
ODBC_CONFIG_SYS_DSN: Modify an existing system data source.
ODBC_REMOVE_SYS_DSN: Remove an existing system data source.
ODBC_REMOVE_DEFAULT_DSN: Remove the default data source specification section from the system information. (It also removes the default driver specification section from the Odbcinst.ini entry in the system information. This fRequest performs the same function as the deprecated SQLRemoveDefaultDataSource function.) When this option is specified, all of the other parameters in the call to SQLConfigDataSource should be NULLs; if they are not NULL, they will be ignored.
lpszDriver
[Input]
Driver description (usually the name of the associated DBMS) presented to users instead of the physical driver name.
lpszAttributes
[Input]
List of attributes in the form of keyword-value pairs. For more information, see ConfigDSN in Chapter 22: Setup DLL Function Reference.
Returns
The function returns TRUE if it is successful, FALSE if it fails. If no entry exists in the system information when this function is called, the function returns FALSE.
方法之一:修改注册表
一般情况下,用户在控制面板中配置好ODBC数据源后,Windows系统会将用户配置的信息写在注册表中。当应用程序需要用到数据源时,Windows会通知底层接口查阅注册表中该数据源的配置。例如,如果配置了系统数据源,Windows系统会根据配置修改注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI 主键。因此,我们只要在应用程序中使用API函数来完成Windows对注册表所做的工作,就可以达到动态加载数据源的目的。
对于不同类型的数据源,注册表修改的内容也会各有不同,但基本上都要修改两个地方:一个是在ODBC.INI子键下建立与数据源配置相关的项;另一个是在\ODBC.INI\ODBC Data Source子键下建立一个新项,以告诉驱动程序管理器ODBC数据源的类型信息。
下面以配置一个ODBC系统数据源为例,给出一段动态加载Access数据源的VB例程:
Public Function LoadDbSource(StrSourceName, StrSourceDB, StrDescription As String) As Boolean
Dim hKey As Long '要打开的注册表键句柄
Dim lReturn As Long '注册表API函数的执行返回值
Dim StrSubKey As String '要打开的子键
Dim Buffer As String * 255
Dim StrSysDir As String
Dim StrDbq, StrDriver, StrFil, StrUid As String
Dim LngDriverID, LngSafeTransactions As Long
Dim StrImplict, StrUserCommit As String
Dim LngPageTimeout, LngThreads, LngMaxBufferSize As Long
Dim StrDbType As String
'检测是否安装 Access ODBC 驱动 odbcjt32.dll
lReturn = GetSystemDirectory(Buffer, 255)
StrSysDir = Left(Buffer, lReturn) '获取Windows系统目录
StrDriver = StrSysDir & "\odbcjt32.dll"
If Dir(StrDriver) = "" Then
MsgBox "您的计算机中没有安装Access的ODBC驱动程序odbcjt32.dll,无法加载数据源。", vbOKOnly + vbCritical
LoadDbSource = False
Exit Function
End If
StrSubKey = "SOFTWARE\ODBC\ODBC.INI" & "\" & StrSourceName
lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, StrSubKey, hKey)
If lReturn <> ERROR_SUCCESS Then
LoadDbSource = False
Else
StrDbq = StrSourceDB
LngDriverID = 25
StrFil = "MS Access;" '文件类型"MS Access" for Microsoft Access
LngSafeTransactions = 0
'设置ODBC数据库引擎名称
lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", 0&, KEY_WRITE, hKey)
If lReturn <> ERROR_SUCCESS Then
LoadDbSource = False
Else
StrDbType = "Microsoft Access Driver (*.mdb)"
lReturn = RegSetValueEx(hKey, StrSourceName, 0&, REG_SZ, StrDbType, LenB(StrDbType))
If lReturn = ERROR_SUCCESS Then LoadDbSource = True
End If
End If
End If
End Function
在上面的程序中使用了标准的Win32 API注册表操作函数,在VB "API文本浏览器"工具和MSDN中可以很容易查到其详细说明。
Private Const ODBC_ADD_SYS_DSN = 4
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
Public Function LoadDbSource2(StrDriver, StrAttributes As String) As Boolean
LoadDbSource2 = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, StrDriver, StrAttributes)
End Function