Table - obtendo extrutura

Top  Previous  Next

Obter a estrutura dos campos de um DB:

**************************************

 

procedure GetTableStruct(dbName, tblName : String; const FldList : TStrings);

 

  const FieldTypeNames: array[ftUnknown..ftTypedBinary] of string =

      ('Unknown', 'String', 'Smallint', 'Integer',

      'Word', 'Boolean', 'Float', 'Currency',

      'BCD', 'Date', 'Time', 'DateTime', 'Bytes',

      'VarBytes', 'AutoInc', 'Blob', 'Memo', 'Graphic',

      'FmtMemo', 'ParadoxOle', 'DBaseOle', 'TypedBinary');

 

var

  I, Idx: Integer;

  FldDef: String;

  Tbl   : TTable;

begin

 

  {Initialize vars}

  FldDef := '';

  Tbl := TTable.Create(Application);

 

  {Now search through the field defs.}

  with tbl do begin

    Active        := False;

    DatabaseName  := dbName;

    TableName     := tblName;

    Open;

    for I := 0 to FieldCount - 1 do begin

 {Get the properties we want to use for the listing}

      FldDef := Fields[i].DisplayLabel + ' ' +

                FieldTypeNames[Fields[I].DataType] + ' ' +

                IntToStr(Fields[I].Size);

 

 {Now, get the Index definitions for the table to determine if a

       field is a primary key or not.}

      IndexDefs.Update;

      if Fields[I].IsIndexField then begin

        Idx := IndexDefs.Indexof(Fields[I].Name);

        if (Idx > -1) then

          if ixPrimary in IndexDefs[Idx].Options then

            FldDef := FldDef + ' *';

      end;

      FldList.Add(FldDef);

    end;

    Free;

  end;

end;

 

 

Exemplo:

********

 

  GetTableStruct('C:\PROFIT2\DADOS', 'CLIENTES.DB', Memo1.Lines);