Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

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

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
Khaki
Asked:
Khaki
  • 5
  • 2
  • 2
  • +1
1 Solution
 
geobulCommented:
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
 
geobulCommented:
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
 
geobulCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
geobulCommented:
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
 
TheLeaderCommented:
"geobul" you did not leave a gap in your answer ;)
good work
[not tested, just Mind_Compiler]
0
 
wilmsoftCommented:
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
 
KhakiAuthor Commented:
geobul and all,
 Thanks to all who answer.  I am just going to test out the answer first.

K
0
 
geobulCommented:
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
 
wilmsoftCommented:
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
 
KhakiAuthor Commented:
Thanks!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now