Einzelnen Beitrag anzeigen

Benutzerbild von Jelly
Jelly

Registriert seit: 11. Apr 2003
Ort: Moestroff (Luxemburg)
3.741 Beiträge
 
Delphi 2007 Professional
 
#5

Re: Tabellen einer DB auflisten über ADO

  Alt 27. Sep 2005, 10:43
Hab mich an die TADOConnection.GetTablesNames angelehnt, und bin zu folgendem funktionierendem Code gelangt.
Delphi-Quellcode:
procedure TMailTableName.FillTableNames(Names: TStringList);
var
  aRS : _RecordSet;
  aConn : _Connection ;
  aOleV : OleVariant;
  sSQL : string ;
  FConnection : string ;
  Info : TMailDatabaseInfo ;
  i : integer ;
  TypeID, NameID : integer ;
  TableType : string ;
begin
  Info := TMailDatabaseInfo (GetComponent(0)) ;
  aRS := CoRecordSet.Create;
  aConn := CoConnection.Create as _Connection ;


  try
    aConn.ConnectionString := Info.ConnectionString ;
    aConn.Open(Info.ConnectionString, '', '', adConnectUnspecified);

    aRS := aConn.OpenSchema(adSchemaTables,EmptyParam,EmptyParam) ;
    TypeID := -1 ;
    NameID := -1 ;
    Names.clear ;

    for i := 0 to aRS.Fields.Count-1 do begin
       if uppercase(aRS.Fields[i].Name) = 'TABLE_TYPEthen TypeID := i ;
       if uppercase(aRS.Fields[i].Name) = 'TABLE_NAMEthen NameID := i ;
    end ;

    while not aRS.EOF do begin
        TableType := uppercase (aRS.Collect[TypeID]) ;
        if ((TableType = 'TABLE') or (TableType = 'VIEW'))
            and (TableType <> 'SYSTEM TABLE') and (TableType <> 'SYSTEM VIEW')
        then Names.Add(aRS.Collect[NameID]) ;
        aRS.MoveNext ;
    end ;
    aRS.Close;
    aConn.close ;
  except

  end;
end;
Klappt zumindest für den MSSQL Server einwandfrei... Sollte aber prinzipiell auch für andere DB klappen.
  Mit Zitat antworten Zitat