怎样检查某个odbc驱动是否安装
如题,有没有什么办法检查某个odbc驱动是否按转并得到相关的信息
比如说,是否安装了mysql odbc 5.0, 或者说得到已经安装了的odbc驱动列表并得到相关信息,比如说版本号
最好不要用数据库控件尝试连接失败得到结果
而是比如说在注册表里找到相关信息?
[解决办法]
- Delphi(Pascal) code
////////////////////////////////////////////////////////////////////////////////// //// CheckMyODBCDriver //// ----------------- //// //// Checks installation of the MyODBC driver //// //// Input: //// (none) //// //// Result [Byte]: //// 0: MySQL driver installed //// 1: missing registry values //// 2: missing file myodbc.dll //// //////////////////////////////////////////////////////////////////////////////////function CheckMyODBCDriver: Byte; var fReg: tRegistry; bDriversValue, bMySQLKey, bMySQLValues: Boolean; sDriver, sSetup: String; begin // checking the registry try fReg := TRegistry.Create; fReg.RootKey := HKEY_LOCAL_MACHINE; // checking key entry \Software\ODBC\ODBCINST.INI\ODBC Drivers fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\ODBC Drivers', True ); bDriversValue := fReg.ValueExists( 'MySQL' ) and ( fReg.ReadString( 'MySQL' ) = 'Installed' ); // checking key entry \Software\ODBC\ODBCINST.INI\MySQL bMySQLKey := fReg.KeyExists( '\Software\ODBC\ODBCINST.INI\MySQL' ); // if exists key entry \Software\ODBC\ODBCINST.INI\MySQL // check also the values if bMySQLKey then begin fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\MySQL', True ); bMySQLValues := fReg.ValueExists( 'APILevel' ) and fReg.ValueExists( 'ConnectFunctions' ) and fReg.ValueExists( 'Driver' ) and fReg.ValueExists( 'DriverODBCVer' ) and fReg.ValueExists( 'FileExtns' ) and fReg.ValueExists( 'FileUsage' ) and fReg.ValueExists( 'Setup' ) and fReg.ValueExists( 'SQLLevel' ); if bMySQLValues then begin sDriver := Trim( fReg.ReadString( 'Driver' ) ); sSetup := Trim( fReg.ReadString( 'Setup' ) ); end; end else bMySQLValues := False; if ( bDriversValue and bMySQLKey and bMySQLValues ) then Result := 0 else Result := 1; fReg.CloseKey; fReg.Free; except Result := 1; fReg.Free; Exit; end; // if registry entries OK => check driver files ... if ( Result = 0 ) then begin if not ( FileExists( sDriver ) and FileExists( sSetup ) ) then Result := 2; end; end;
[解决办法]
简单的,可直接使用SQLGetPrivateProfileString
//------------------------
- Delphi(Pascal) code
Function SQLGetPrivateProfileString ( lpszSection :pansichar; lpszEntry:pansichar; lpszDefault:pansichar; RetBuffer:pansichar; cbRetBuffer:integer; lpszFilename:pansichar) :integer;stdcall;external 'ODBCCP32.DLL';rocedure TForm1.Button1Click(Sender: TObject);var sqBuffer:ansipchar; len:integer;begin getmem(sqBuffer,1024); len:=SQLGetPrivateProfileString ('MySQL ODBC 5.1 Driver',nil,'', sqBuffer,1024,'odbcinst.ini'); if len<>0 then ShowMessage('MySQL ODBC 5.1 Driver 驱动已安装!') else ShowMessage('MySQL ODBC 5.1 Driver驱动未安装!'); freeMem(sqBuffer);end;