Delphi .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Retrieve all tables in a database with ADO
This article has not been rated yet. After reading, feel free to leave comments and rate it.
The following code enumerates all tables and views in a database. ADO distinguishes between these table types:
- Table
- View
- Synonym
- System Table
- Access Table
The supplied unit defines matching constants and function ADODbTables can be used as shown in the button click handler at the bottom. | |  | | unit dbTables;
interface
uses
ADODb;
type
TTableType = (ttTable, ttView, ttSynonym, ttSystemTable, ttAccessTable);
TTableTypes = set of TTableType;
TTableItem = record
ItemName: string;
ItemType: string;
end;
TTableItems = array of TTableItem;
function addFilter(string1, string2: string) : string;
function ADODbTables(ADOConnection: TADOConnection; types: TTableTypes) : TTableItems;
implementation
function addFilter(string1, string2: string) : string;
begin
if string1<>'' then
Result := string1 + ' or ' + string2
else
Result := string2
end;
function ADODbTables(ADOConnection: TADOConnection; types: TTableTypes) : TTableItems;
var
ADODataSet: TADODataSet;
i : integer;
begin
ADODataSet := TADODataSet.Create(nil);
ADODataSet.Connection := ADOConnection;
ADOConnection.OpenSchema(siTables, EmptyParam, EmptyParam, ADODataSet);
if (ttTable in types) then
ADODataSet.Filter := addFilter(ADODataSet.Filter, '(TABLE_TYPE = ''TABLE'')');
if (ttView in types) then
ADODataSet.Filter := addFilter(ADODataSet.Filter, '(TABLE_TYPE = ''VIEW'')');
if (ttSynonym in types) then
ADODataSet.Filter := addFilter(ADODataSet.Filter, '(TABLE_TYPE = ''SYNONYM'')');
if (ttSystemTable in types) then
ADODataSet.Filter := addFilter(ADODataSet.Filter, '(TABLE_TYPE = ''SYSTEM TABLE'')');
if (ttAccessTable in types) then
ADODataSet.Filter := addFilter(ADODataSet.Filter, '(TABLE_TYPE = ''ACCESS TABLE'')');
ADODataSet.Filtered := True;
SetLength(Result, ADODataSet.RecordCount);
i := 0;
with ADODataSet do
begin
First;
while not EOF do
begin
with Result[i] do
begin
ItemName := FieldByName('TABLE_NAME').AsString;
ItemType := FieldByName('TABLE_TYPE').AsString
end;
Inc(i);
Next
end;
end;
ADODataSet.Free
end;
end.
// set | |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|