Board index » delphi » odbc driver list

odbc driver list

Does anyone have a snippet of code that will give me a list of ODBC drivers
installed on the current machine?  I need it for NT 4.0, but it would be
nice for it to work on Win 95/98 also.

thanks,
Brad Irby
Building Block Software

 

Re:odbc driver list


Brad Irby wrote

Quote
>Does anyone have a snippet of code that will give me a list of ODBC drivers
>installed on the current machine?

Here is a Code snippet:

type
  PSQLCHAR = ^SQLCHAR;
  SQLCHAR = Char;
  SQLSCHAR = Char;
  SQLINTEGER = Longint;
  SQLPOINTER = Pointer;
  SQLSMALLINT = Smallint;

{ function return type }
type
  SQLRETURN = SQLSMALLINT;

{ generic data structures }
type
  SQLHANDLE = Pointer;
  SQLHENV = SQLHANDLE;

const
  SQL_FETCH_NEXT = 1;
  SQL_FETCH_FIRST = 2;

function SQLAllocEnv(var EnvironmentHandle: SQLHENV):
SQLRETURN;stdcall;external  'odbc32.dll';
function SQLFreeEnv(var EnvironmentHandle: SQLHENV):
SQLRETURN;stdcall;external  'odbc32.dll';

function SQLDrivers (henv: SQLHENV;
                       fDirection: SQLUSMALLINT;
                       szDriverDesc: PSQLCHAR;
                       cbDriverDescMax: SQLSMALLINT;
                       var pcbDriverDesc: SQLSMALLINT;
                       szDriverAttributes: PSQLCHAR;
                       cbDrvrAttrMax: SQLSMALLINT;
                       var pcbDrvrAttr:
SQLSMALLINT):SQLRETURN;stdcall;external 'odbc32.dll';

procedure GetDrivers(HENV : SQLHENV; List : TStrings);
var
  DriverName : array[0..255] of SQLCHAR;
  NameLen : SQLSMALLINT;
  Attribs : array[0..255] of SQLCHAR;
  AttrLen : SQLSMALLINT;
  Status : SQLRETURN;
begin
  FillChar(DriverName[0], 256, #0);
  FillChar(Attribs[0], 256, #0);
  Status := SQLDrivers(HENV, SQL_FETCH_FIRST, @DriverName[0], 255,
            NameLen, @Attribs[0], 255, AttrLen);
  while (Status = 0) do
  begin
    List.Add(String(DriverName));
    Status := SQLDrivers (HENV, SQL_FETCH_NEXT, @DriverName[0], 255,
NameLen,
            @Attribs[0], 255, AttrLen);
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  GlblHEnv : SQLHENV;
begin
  GlblHEnv := nil;
  if SQLAllocEnv(GlblHEnv) <> 0 then Exit;
  GetDrivers(GlblHEnv, ListBox1.Items);
  SQLFreeEnv(GlblHEnv);
end;

-- Reddy.

Re:odbc driver list


Cool snippet!  Worked right out of the box.  Thanks.

I mis-spoke in my message, though.  What I need is something to give me a
list of datasources.  Would you happen to have that?

Also, where did you find documentation for all this?  I've looked in the
ODBC help files and can't find anything.  There were a few calls documented
in the Jet ODBC help, but not the ones you used here.  Is there a book
somewhere you'd suggest?

thanks again,
brad

Quote
Reddy Palle <palle.re...@mpactsystems.com> wrote in message

news:7kubjq$dsu5@forums.borland.com...
Quote

> Brad Irby wrote
> >Does anyone have a snippet of code that will give me a list of ODBC
drivers
> >installed on the current machine?

> Here is a Code snippet:

> type
>   PSQLCHAR = ^SQLCHAR;
>   SQLCHAR = Char;
>   SQLSCHAR = Char;
>   SQLINTEGER = Longint;
>   SQLPOINTER = Pointer;
>   SQLSMALLINT = Smallint;

> { function return type }
> type
>   SQLRETURN = SQLSMALLINT;

> { generic data structures }
> type
>   SQLHANDLE = Pointer;
>   SQLHENV = SQLHANDLE;

> const
>   SQL_FETCH_NEXT = 1;
>   SQL_FETCH_FIRST = 2;

> function SQLAllocEnv(var EnvironmentHandle: SQLHENV):
> SQLRETURN;stdcall;external  'odbc32.dll';
> function SQLFreeEnv(var EnvironmentHandle: SQLHENV):
> SQLRETURN;stdcall;external  'odbc32.dll';

> function SQLDrivers (henv: SQLHENV;
>                        fDirection: SQLUSMALLINT;
>                        szDriverDesc: PSQLCHAR;
>                        cbDriverDescMax: SQLSMALLINT;
>                        var pcbDriverDesc: SQLSMALLINT;
>                        szDriverAttributes: PSQLCHAR;
>                        cbDrvrAttrMax: SQLSMALLINT;
>                        var pcbDrvrAttr:
> SQLSMALLINT):SQLRETURN;stdcall;external 'odbc32.dll';

> procedure GetDrivers(HENV : SQLHENV; List : TStrings);
> var
>   DriverName : array[0..255] of SQLCHAR;
>   NameLen : SQLSMALLINT;
>   Attribs : array[0..255] of SQLCHAR;
>   AttrLen : SQLSMALLINT;
>   Status : SQLRETURN;
> begin
>   FillChar(DriverName[0], 256, #0);
>   FillChar(Attribs[0], 256, #0);
>   Status := SQLDrivers(HENV, SQL_FETCH_FIRST, @DriverName[0], 255,
>             NameLen, @Attribs[0], 255, AttrLen);
>   while (Status = 0) do
>   begin
>     List.Add(String(DriverName));
>     Status := SQLDrivers (HENV, SQL_FETCH_NEXT, @DriverName[0], 255,
> NameLen,
>             @Attribs[0], 255, AttrLen);
>   end;

> end;

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   GlblHEnv : SQLHENV;
> begin
>   GlblHEnv := nil;
>   if SQLAllocEnv(GlblHEnv) <> 0 then Exit;
>   GetDrivers(GlblHEnv, ListBox1.Items);
>   SQLFreeEnv(GlblHEnv);
> end;

> -- Reddy.

Re:odbc driver list


Brad Irby wrote

Quote
>What I need is something to give me a
>list of datasources.  Would you happen to have that?

Add the following in the implementation section.

function SQLDataSources(EnvironmentHandle: SQLHENV;
                    Direction: SQLUSMALLINT;
                    ServerName: PSQLCHAR;
                    BufferLength1: SQLSMALLINT;
                    var NameLength1: SQLSMALLINT;
                    Description: PSQLCHAR;
                    BufferLength2: SQLSMALLINT;
                    var NameLength2: SQLSMALLINT):
SQLRETURN;stdcall;external 'odbc32.dll';

procedure GetDataSourceNames(HEnv : SQLHENV; List : TStrings);
var
  DSNName : array[0..32] of Char;
  Driver  : string;
  Desc    : array[0..255] of Char;
  Status  : SQLRETURN;
  Len1, Len2 : SQLSMALLINT;
begin
  FillChar(DSNName[0], 32, #0);
  FillChar(Desc[0], 255, #0);
  Status := SQLDatasources(HEnv, SQL_FETCH_FIRST, @DSNName[0],
Length(DSNName), Len1, @Desc[0],
            Length(Desc), Len2);
  //if RC = SQL_NO_DATA then Exit;
  while (Status = 0) do
  begin
    List.Add(DSNName);
    FillChar(DSNName[0], 32, #0);
    FillChar(Desc[0], 255, #0);
    Status := SQLDatasources(HEnv, SQL_FETCH_NEXT, @DSNName[0],
Length(DSNName), Len1, @Desc[0],
             Length(Desc), Len2);
  end;
end;

Replace the Button1click with the follwing :

procedure TForm1.Button1Click(Sender: TObject);
var
  GlblHEnv : SQLHENV;
begin
  GlblHEnv := nil;
  if SQLAllocEnv(GlblHEnv) <> 0 then Exit;
  {GetDrivers(GlblHEnv, ListBox1.Items);}
  GetDataSourceNames(GlblHEnv, ListBox1.Items);
  SQLFreeEnv(GlblHEnv);
end;

Quote
>Also, where did you find documentation for all this?  I've looked in the
>ODBC help files and can't find anything.  There were a few calls documented
>in the Jet ODBC help, but not the ones you used here.  Is there a book
>somewhere you'd suggest?

These are documented in the MSDN and you have to translate the ODBC header
files manually yourself. If you have any C/C++ compiler you can look in the
header files for Sql.h, SqlExt.h, Sqltypes.h.
I don't have any specific book but you can read the "Inside ODBC" in MSDN if
you have subsription. You can also browse the MSDN from MS website :
http://msdn.microsoft.com/library/sdkdoc/dasdk/odp175gx.htm

-- Reddy.

Other Threads