Home | Chi sono | Contattami
 

Progr. lineare

Delphi
 
Componenti
  Database
 
Miei articoli

Windows

Miei articoli 

 

Enumerare tutte le tabelle di un database incluse quelle di sistema


Non c'è molto da dire, il titolo dice già tutto ...

1. Unit implementativa

unit dbTables; interface uses ADODb; type TTableType = (ttTable, ttView, ttSynonym, ttSystemTable, ttAccessTable); type TTableTypes = set of TTableType; type TTableItem = record ItemName: string; ItemType: string; end; type 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.

2. Esempio

Crea un nuovo progetto e aggiungi i seguenti ingredienti: ADOConnection1 (TADOConnection), Button1 (TButton) e Memo1 (TMemo); assegna un valore alla proprietà ConnectionString del componente ADOConnection1 ed imposta ADOConnection1.Active := True: si può utilizzare ad esempio un database in Access e giocare un pò coi vari tipi di tabelle (ttTable, ttSystemTable, etc...)

procedure TForm1.Button1Click(Sender: TObject); var output: ttableitems; i: integer; begin output := ADODbTables(ADOConnection1, [ttTable, ttView, ttSynonym]); for i := Low(output) to High(output) do begin Memo1.Lines.Add(output[i].ItemName + '---' + output[i].ItemType); end; output := nil; end;

 


 

 

 

 
 
Your Ad Here