Create TTable without a form

Posted in Databases

Actually you don't need to use any visual component. Remember that each component in Delphi is an object and do not forget to free it for releasing of the allocated memory.

uses Db, DBTables, Dialogs

procedure TForm1.Button1Click(Sender: TObject);
var
  Session: TSession;
  Database: TDatabase;
  Table: TTable;
begin
  Session:=TSession.Create(nil);
  Database:=TDatabase.Create(nil);
  Session.SessionName:='DBSession';
  Database.Connected:=False;
  Database.AliasName:='dbdemos';
  Database.DatabaseName:='biolife';
  Database.SessionName:=Session.SessionName;

  Table:=TTable.Create(nil);
  Table.DatabaseName:=Database.DatabaseName;
  Table.SessionName:=Session.SessionName;
  Table.TableName:='biolife';

  Table.Active:=True;
  ShowMessage(Table.Fields[1].AsString+' '+Table.Fields[2].AsString);
  Table.Free;
  Database.Free;
  Session.Free;
end;