如何获取给定ODBC驱动程序的ODBC驱动程序的DLL文件的名称 [英] How to get name of ODBC driver's DLL file for a given ODBC driver

查看:126
本文介绍了如何获取给定ODBC驱动程序的ODBC驱动程序的DLL文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式获取给定ODBC驱动程序的ODBC驱动程序的DLL文件的名称.例如,给定"SQL Server Native Client 10.0",我想找到该驱动程序的DLL文件的名称:sqlncli10.dll.我可以在注册表中HKEY_LOCAL_MACHINE \ SOFTWARE \ ODBC \ ODBCINST.INI下的驱动程序"项中的REGEDIT中看到这一点.如果我尝试从代码中的注册表中读取值,它将返回一个空字符串.我也尝试使用ODBC API函数SQLDrivers.下面的代码成功返回Attribs变量中除"Driver"之外的所有属性值.一切都在这里-APILevel,ConnectFunctions,CPTimeout等-但驱动程序"不在列表中.

How do I programatically get the name of an ODBC driver's DLL file for a given ODBC driver. For example, given "SQL Server Native Client 10.0" I want to find the name of that driver's DLL file: sqlncli10.dll. I can see this in REGEDIT in the "Driver" entry in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI. If I try to read the value from the registry in my code it returns an empty string. I also tried using the ODBC API function SQLDrivers. The code below successfully returns all the values of the attributes in the Attribs variable except "Driver". Everything is there - APILevel, ConnectFunctions, CPTimeout, etc - but "Driver" is not in the list.

repeat
  Status := SQLDrivers (HENV, SQL_FETCH_NEXT, PAnsiChar(DriverName), 255,
            NameLen, PAnsiChar(Attribs), 1024, AttrLen);
  if Status = 0 then begin
    List.Add(DriverName);
    List.Add(Attribs);
  end;
until Status <> 0;

推荐答案

您可以将SQLGetInfo()InfoType=SQL_DRIVER_NAME

我希望它看起来像这样:

I hope this will look like:

Status := SQLGetInfo(ConnEnv, SQL_DRIVER_NAME, PAnsiChar(DriverName), 255, NameLen);

但是此功能适用于已连接的数据库.

But this function works with already connected database.

我尝试了SQLDrives(),而您是正确的:在我的环境中,该函数也不返回DLL名称.因此,我尝试从注册表中读取它,并以这种方式工作:

I tried SQLDrives() and you are right: in my environment this function also do not return DLL name. So I tried to read it from registry and it worked this way:

  DLLName := RegGetStringDirect(HKEY_LOCAL_MACHINE, 'SOFTWARE\ODBC\ODBCINST.INI\'  + DriverName, 'Driver');

对于驱动程序:IBM INFORMIX ODBC DRIVER我得到了:C:\informix\bin\iclit09b.dll

For driver: IBM INFORMIX ODBC DRIVER I got: C:\informix\bin\iclit09b.dll

对于驱动程序:SQL Server我得到了:C:\WINDOWS\system32\SQLSRV32.dll

For driver: SQL Server I got: C:\WINDOWS\system32\SQLSRV32.dll

RegGetStringDirect()是我基于Windows API的功能,用于从注册表中读取内容.

RegGetStringDirect() is my function based on Windows API to read something from registry.

由Ron Schuster读取"SQL Server" ODBC驱动程序dll名称的两个功能已从注释中移出:

Two functions to read "SQL Server" ODBC driver dll name by Ron Schuster moved from comment:

procedure TForm1.Button1Click(Sender: TObject); 
//using Windows API calls 
var 
  KeyName, ValueName, Value: string; 
  Key: HKEY; 
  ValueSize: Integer; 
begin
  ValueName := 'Driver';
  KeyName := 'SOFTWARE\ODBC\ODBCINST.INI\SQL Native Client';
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar(KeyName), 0, KEY_READ, Key) = 0 then
    if RegQueryValueEx(Key, PChar(ValueName), nil, nil, nil, @ValueSize) = 0 then begin
      SetLength(Value, ValueSize);
      RegQueryValueEx(Key, PChar(ValueName), nil, nil, PByte(Value), @ValueSize);
      ShowMessage(Value);
    end;
end; 

procedure TForm1.Button2Click(Sender: TObject);
//using TRegistry class 
var
  KeyName, ValueName, Value: string;
  Reg: TRegistry;
begin
  ValueName := 'Driver';
  KeyName := 'SOFTWARE\ODBC\ODBCINST.INI\SQL Native Client';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKeyReadOnly(KeyName) then begin
      Value := Reg.ReadString(ValueName);
      ShowMessage(Value);
    end;
  finally
    Reg.Free;
  end;
end;

这篇关于如何获取给定ODBC驱动程序的ODBC驱动程序的DLL文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆