Page 1 of 1

Get ODBC Datasourcenames?

Posted: Tue 18 Nov 2014 12:03
by mika
Is there anyway to get list of data sources names registered with ODBC Administrator (User DSN or System DSN).

I tried with UniConnection.GetDatabaseNames(List), but I only receive error [Microsoft][ODBC Ohjaintenhallinta] Datasource name not found and default driver is not defined. (Sorry might not be correct error, this is translated from Finnish)

Re: Get ODBC Datasourcenames?

Posted: Thu 20 Nov 2014 09:50
by AlexP
Hello,

You can retrieve the list of registered DSN using the UniConnectDialog.GetServerList method:

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Uni, ODBCUniProvider, UniDacVcl;

var
  UniConnection: TUniConnection;
  UniConnectDialog: TUniConnectDialog;
  Lst: TStringList;
  i: integer;
begin
  UniConnection := TUniConnection.Create(nil);
  try
    UniConnection.ProviderName := 'ODBC';
    UniConnectDialog := TUniConnectDialog.Create(nil);
    try
      UniConnection.ConnectDialog := UniConnectDialog;
      UniConnectDialog.UseServerHistory := False;
      Lst := TStringList.Create;
      try
        UniConnectDialog.GetServerList(Lst);
        for i := 0 to Lst.Count - 1 do
          Writeln(Lst[i]);
      finally
        Lst.Free;
      end;
    finally
      UniConnectDialog.Free;
    end;
  finally
    UniConnection.Free;
    readln;
  end;
end.