Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Retrieve DSN names and ODBC Drivers listFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Database Others
Skill Level:
Scoring:
Last Update:
03/15/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet DSN ODBC-DRIVERS ODBC Registry
Times Scored:
10
Visits:
8389
Uploader: Oscar Noe Martin
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to get a list of DSNs in system?
Answer:



I didn't try it in all delphi versions but must work... ;)


unit DSNFuncs;

interface
Uses Windows, Classes, SysUtils, Registry;

Type
  TDSN_Type       = (dtUSER, dtSYSTEM);
  TDSN_Types      = Set of TDSN_Type;


function GetODBCDriversList: TStrings;
function GetDSNList (aDSN_TYPES: TDSN_Types; aDriverFilter: String): TStrings;

implementation

function GetODBCDriversList: TStrings;

  // This function returns a list of currently installed ODBC drivers
  // Example: combobox1.Assign(GetODBCDriversList); // ;)

var
  aStringlist   : TStringlist;
  aRegistry   : TRegistry;
Begin
  aStringlist:= Tstringlist.Create;
  aRegistry:= TRegistry.Create;
  Result:= Tstringlist.Create;

  with aRegistry do
  Begin
    rootkey:= HKEY_LOCAL_MACHINE;
    OpenKey('Software\ODBC\ODBCINST.INI\ODBC Drivers',False);
    GetValueNames(aStringlist);
  End;
  aRegistry.Free;
  aStringlist.Sort;
  result.AddStrings(aStringlist);
  aStringlist.Free;
End;

function GetDSNList (aDSN_TYPES: TDSN_Types; aDriverFilter: String): TStrings;

  // This function returns a list of dsn( user's dsn (dtUser),
  // system's dsn (dtSystem), or both), where driver contains aDriverFilter,
  // it can be complete driver name or part of it;
  //       (Example: 'Microsoft Access Driver (*.mdb)' or 'Access').
  //
  // Example: listbox1.Items.Assign(GetDSNList([dtUser,dtSystem],'Access');
  // It will return a list with all existing DSN both from User and System with
  // string 'Access' in driver's name.

var
  aStringList : TStringlist;

  aStrings1   : TStrings;
  aStrings2   : Tstrings;
  aString     : String;

  aRegistry   : TRegistry;
  aInt        : Integer;
Begin
  aStringlist:=TStringlist.Create;
  aStrings1:=TStringlist.Create;
  aStrings2:=TStringlist.Create;

  If dtUSER in aDSN_TYPES then
  Begin
    aRegistry:= Tregistry.Create;
    With aRegistry do
    Begin
      RootKey:=HKEY_CURRENT_USER;
      OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',False);
      GetValueNames(aStrings1);
      for aInt:=aStrings1.Count-1 downto 0 do
      Begin
        aString:= ReadString(aStrings1.Strings[aInt]);
        if ((Pos( aDriverFilter, aString ) = 0) and (aDriverFilter<>''))  then
          aStrings1.Delete(aInt);
      End;
    End;
    aRegistry.Free;
  end;

  If dtSYSTEM in aDSN_TYPES then
  Begin
    aRegistry:= Tregistry.Create;
    With aRegistry do
    Begin
      RootKey:=HKEY_LOCAL_MACHINE;
      OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',False);
      GetValueNames(aStrings2);
      for aInt:=aStrings2.Count-1 downto 0 do
      Begin
        aString:= ReadString(aStrings2.Strings[aInt]);
        if ((Pos( aDriverFilter, aString) = 0) and (aDriverFilter<>''))  then
          aStrings2.Delete(aInt);
      End;
    End;
    aRegistry.Free;
  end;

  aStringlist.AddStrings(aStrings1);
  aStrings1.Free;
  aStringlist.AddStrings(aStrings2);
  aStrings2.Free;
  aStringlist.Sort;
  result:= Tstringlist.Create;
  result.Assign(aStringlist);
End;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Retrieving Server Name from a ODBC DSN?
    MohammadReza ZarrinPour (Apr 23 2006 5:46AM)

Dear Sir Oscar Noe Martin,
Thanks for your instructive Article "Retrieve DSN names and ODBC Drivers list" really it saved my project.!!!
BUT i have a diffrent question: How can i retrieve the HOST or Server Name for a given ODBC DSN Connection.?

I appreciate your soonest reply.
Regards.

Respond

use ODBC32.dll
    Mike Shkolnik (Mar 26 2003 3:00PM)

Maybe I'm wrong but IMHO better to use SQLDataSources and SQLDrivers functions from ODBC32.dll:

   function SQLDataSources(
               EnvironmentHandle:SQLHENV;
               Direction:SQLUSMALLINT;
               ServerName:PSQLCHAR;
               BufferLength1:SQLSMALLINT;
               NameLength1:PSQLSMALLINT;
               Description:PSQLCHAR;
               BufferLength2:SQLSMALLINT;
               NameLength2:PSQLSMALLINT): SQLRETURN; stdcall;external 'ODBC32.DLL';

   function SQLDrivers(
               EnvironmentHandle:SQLHENV;
               Direction:SQLUSMALLINT;
               DriverDescription:PSQLCHAR;
               BufferLength1:SQLSMALLINT;
               DescriptionLength1:PSQLSMALLINT;
               DriverAttributes:PSQLCHAR;
               BufferLength2:SQLSMALLINT;
               AttributesLength2:PSQLSMALLINT): SQLRETURN; stdcall;external 'ODBC32.DLL';

Respond

RE: use ODBC32.dll
dreel dr (Nov 9 2006 2:36PM)

Can you write an example of use?
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)