能动态配置ODBC数据源吗?

viciner 2003-02-18 03:50:48
让客户配置这些东西会要了他们的命的!!
...全文
140 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yonghengdizhen 2003-02-18
  • 打赏
  • 举报
回复
动态加载ODBC数据源没这么麻烦,一个SQLConfigDataSource调用就OK了..
ODBC Programmer's Reference

SQLConfigDataSource
Conformance
Version Introduced: ODBC 1.0

Summary

SQLConfigDataSource adds, modifies, or deletes data sources.

Syntax

BOOL SQLConfigDataSource(
HWND hwndParent,
WORD fRequest,
LPCSTR lpszDriver,
LPCSTR lpszAttributes);
Arguments

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.

把这个api转换成VB的声明,调用就OK了,该函数存在于odbccp32.dll中..
holydiablo 2003-02-18
  • 打赏
  • 举报
回复
在VB中动态加载ODBC数据源

  在编写数据库应用程序时,常常需要用户自己在控制面板中配置ODBC数据源。然而对一般用户而言,配置ODBC数据源的工作是有一定困难的。因此,如果能在程序中动态加载ODBC数据源,就能大大方便应用程序最终用户的使用。本文介绍在VB中动态加载ODBC数据源的两种方法。

方法之一:修改注册表
  一般情况下,用户在控制面板中配置好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

Call RegSetValueEx(hKey, "DBQ", 0&, REG_SZ, StrDbq, LenB(StrDbq))
Call RegSetValueEx(hKey, "Description", 0&, REG_SZ, StrDescription, LenB(StrDescription))
Call RegSetValueEx(hKey, "Driver", 0&, REG_SZ, StrSourceDB, LenB(StrSourceDB))
Call RegSetValueEx2(hKey, "DriverID", 0&, REG_DWORD, LngDriverID, 4)
Call RegSetValueEx(hKey, "FIL", 0&, REG_SZ, StrFil, LenB(StrFil))
Call RegSetValueEx(hKey, "UID", 0&, REG_SZ, "", LenB(""))
Call RegSetValueEx2(hKey, "SafeTransaction", 0&, REG_DWORD, LngSafeTransactions, 4)
Call RegCloseKey(hKey)

StrSubKey = StrSubKey & "\Engines\Jet"
lReturn = RegCreateKey(HKEY_LOCAL_MACHINE, StrSubKey, hKey)
If lReturn <> ERROR_SUCCESS Then
LoadDbSource = False
Else
StrImplict = ""
StrUserCommit = "Yes"
LngPageTimeout = 5
LngThreads = 3
LngMaxBufferSize = 2048

Call RegSetValueEx(hKey, "ImplictCommitSync", 0&, REG_SZ, StrImplict, LenB(StrImplict))
Call RegSetValueEx2(hKey, "MaxBufferSize", 0&, REG_DWORD, LngMaxBufferSize, 4)
Call RegSetValueEx2(hKey, "PageTimeout", 0&, REG_DWORD, LngPageTimeout, 4)
Call RegSetValueEx2(hKey, "Threads", 0&, REG_DWORD, LngThreads, 4)
Call RegSetValueEx(hKey, "UserCommitSync", 0&, REG_SZ, StrUserCommit, LenB(StrUserCommit))
Call RegCloseKey(hKey)

'设置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中可以很容易查到其详细说明。

方法之二:使用ODBC DLL提供的函数
  Windows系统目录中提供有一系列专门操作ODBC的动态链接库,其中Odbcinst.dll提供了一个可以动态的增加、删除和修改数据源的函数SQLConfigDataSource()。微软在MSDN的帮助文档中提供了对该函数的详细使用说明:
BOOL SQLConfigDataSource(
HWND hwndParent,
WORD fRequest,
LPCSTR lpszDriver,
LPCSTR lpszAttributes);
其中:hwndParent为调用窗体句柄;fRequest为操作码,通过设置不同的值可以实现对数据源的增加、删除、修改操作,这些对应值可以在MSDN中查到;lpszDriver参数传递数据库引擎,lpszAtrributes是配置信息列表,配置信息条目之间以字符代码Chr(0)分隔。同样,我们以上面的Access数据源配置为例写一个标准函数:

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

在VB程序中调用这个标准例程:
Dim StrAttributes As String
StrAttributes = "DSN=AccessODBC2" & Chr(0) & "Desciption=动态加载ODBC示例" & Chr(0)
StrAttributes = StrAttributes & "Dbq=" & App.Path & "\MyDb.mdb" & Chr(0) & "FIL=MS Access;" & Chr(0)
StrAttributes = StrAttributes & "MaxBufferSize=2048" & Chr(0) & "PageTimeout=5" & Chr(0)
Call LoadDbSource2("Microsoft Access Driver (*.mdb)", StrAttributes)


  上述两种方法都可以实现VB动态加载ODBC数据源,对于不同类型的数据源,注册表中记录的内容会有一些不同。所以,除查阅有关文档外,程序员可以先通过手工在控制面板中配置数据源,然后再通过观察注册表中的内容再进行编程。

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧