Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to get connect via ODBC and get list of DSN?

Posted on 2003-11-25
10
544 Views
Last Modified: 2010-04-05
I am using ADO right now and I like a way to connect to ODBC database as well.  How would I go about to doing this?  
Also, how can I get a list of DSN?

Examples appreciated.

TIA

K
0
Comment
Question by:Khaki
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 9817019
Hi,

Use 'Microsoft OLE DB Provider for ODBC Drivers' in your connection string. The basic connection string looks like:
Provider=MSDASQL.1;Persist Security Info=False;Data Source=dBASE Files
where 'dBASE Files' is your DSN.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9817074
About getting a list of DSN entries from the registry:

User DSN: read subkeys in HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI
System DSN: read values in HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 9817162
One correction:
User DSN: read values in HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources

The code is:

type
  TForm1 = class(TForm)
    lbDSN: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses Registry;

// get list of user and system DSN
procedure DSNList(DSNList: TStrings);
var
  i: integer;
  sl: TStringList;
begin
 sl := TStringList.Create;
 try
  DSNList.Clear;
  with TRegistry.Create do begin
    try
      // System DSN
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', False) then begin
        GetValueNames(sl);
        if sl.Count > 0 then begin
          for i := 0 to sl.Count - 1 do begin
            DSNList.Add(sl[i]);
          end;
        end;
      end;
      // User DSN
      sl.Clear;
      RootKey := HKEY_CURRENT_USER;
      if OpenKey('SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', False) then begin
        GetValueNames(sl);
        if sl.Count > 0 then begin
          for i := 0 to sl.Count - 1 do begin
            DSNList.Add(sl[i]);
          end;
        end;
      end;
    finally
      Free;
    end;
  end;
 finally
   sl.Free;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DSNList(lbDSN.Items);
end;

end.

Regards, Geo
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:geobul
ID: 9817178
I forgot to close the keys:

...
    if OpenKey('\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', False) then begin
        GetValueNames(sl);
        if sl.Count > 0 then begin
          for i := 0 to sl.Count - 1 do begin
            DSNList.Add(sl[i]);
          end;
        end;
        CloseKey; // add this line here and one for the other key also
      end;

Regards, Geo
0
 
LVL 2

Expert Comment

by:TheLeader
ID: 9818758
"geobul" you did not leave a gap in your answer ;)
good work
[not tested, just Mind_Compiler]
0
 
LVL 1

Expert Comment

by:wilmsoft
ID: 9819164
Give this a try... it  uses DBTables (at least in version 6)


try
  { fill a list box with alias names for the user to select from }
  Session.GetAliasNames(ComboBox1.Items);
except
exit;
end;//try.. execept
0
 

Author Comment

by:Khaki
ID: 9825220
geobul and all,
 Thanks to all who answer.  I am just going to test out the answer first.

K
0
 
LVL 17

Expert Comment

by:geobul
ID: 9825382
Thank you TheLeader :-)

wilmsoft,
TSession requires BDE to be installed and working ! Khaki is using ADO (i.e. no BDE), so that will not be a desired solution I think.

Regards, Geo
0
 
LVL 1

Expert Comment

by:wilmsoft
ID: 9825681
I guess I miss understood the question... I thought he DIDN'T want to use the ADO....

SO, here is how you do connect to a list of DSN via ODBC using the ADO.

try
ADOConnection1.ConnectionString := PromptDataSource(Handle, Edit1.Text);
ADOConnection1.GetTableNames(Listbox1.Items,false);
execpt
end;
0
 

Author Comment

by:Khaki
ID: 9858143
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 104
FMX enumerated colours 2 115
Create Database on Android via Delphi dbExpress 3 111
Delphi: Connect to running MS Outlook 4 118
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question