Board index » delphi » List of tables in a database

List of tables in a database

All,

How can I display a list of tables in a drop-down list from a given database
? Similarly, a list of fields from a given table ?  I'm sure this must be
possible quite easily but I can't see it.

Thanks in advance,

Mike.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

 

Re:List of tables in a database


Quote
>How can I display a list of tables in a drop-down list from a given database
>? Similarly, a list of fields from a given table ?  I'm sure this must be
>possible quite easily but I can't see it.

It depends on the database. Look for the table in your database
dictionary that contains the names of the tables. For example, in
Oracle, it is called User_Tabs.

Re:List of tables in a database


Quote
>How can I display a list of tables in a drop-down list from a given
database
>? Similarly, a list of fields from a given table ?  I'm sure this must be
>possible quite easily but I can't see it.

{ Gets the list of table names for a database name / alias name}
function GetTables(const Alias : string) : TStrings;
begin
  Result := TStringlist.Create;
  Session.GetTableNames(Alias, '*.*', False, False, Result);
end;

{ Gets the list of fields for a given table or dataset}
function GetFields(const DataSet : TDataset) : TStrings
begin
   Result := TStringList.Create;
   GetFieldNames(Result);
end;

{Invoke those functions here}
procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items := GetTables(DataBase1.DatabaseName);
  if ComboBox1.Items.Count <> 0 then
  begin
    ComboBox1.ItemIndex := 0;
    Table1.TableName := Combobox1.Items[ComboBox1.ItemIndex];
    ComboBox2.Items := GetFields(Table1);
  end;
end;
{ end of code }

 Hope this helps.

-- reddy.

Other Threads