When creating a DSN programmatically for an Oracle database, how can I reliably specify the driver name?

I have an application that connects via DSN to an Oracle database. If the initial connection attempt fails, I will make sure their DSN exists. If it doesn't exist, I create it using the SQLConfigDataSource command .

This command requires the name of the driver as one of its arguments. On my machine, I have an 11g driver, so the following works:

const
  cDriver = 'Oracle in OraDb11g_home1' + #0;
var
  strAttr: string;
begin
  strAttr := 'DSN=' + DSNName + #0 +
             'SERVER=' + TNSName + #0;
  SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,PChar(cDriver),PChar(strAttr));
end;

      

But the client machine might have a different version of Oracle or a different name for its oracle home. How can I determine which driver to use on an arbitrary machine?

I am using Delphi but it doesn't really matter as it is just an API call.

+1


a source to share


2 answers


I ended up using a list in the registry as shown below:



function TDSNManager.GetOracleDriverName: string;
var
  reg : TRegistry;
  drivers: TStringList;
  i: integer;
begin
  drivers := TStringList.Create;
  reg := nil;
  try
    reg := TRegistry.Create;
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI',False) then begin
      reg.GetKeyNames(drivers);
    end;
  finally
    FreeAndNil(reg);
  end;  //try-finally

  for i := 0 to drivers.Count - 1 do begin
    if 0 < Pos('ORACLE IN',Uppercase(drivers[i])) then begin
      Result := drivers[i];
      Break;
    end;
  end;
end;

      

+2


a source


You can try delving into the SQLDrivers call to find out about getting the installed drivers on the system you are on.



+1


a source







All Articles