Autor Beitrag
Flash106
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 234


D7 Ent
BeitragVerfasst: So 23.01.05 14:14 
Hallo,

weis jemande wie ich aus einer sql db von programm aus alle Feldnamen, die dazugehörigen Feldtypen und Feldlänge auslesen kann?

Hab nur source für die Feldnamen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
...
var i: integer;
    dbFeldName: string;
begin
  SQlQuery1.Close;

  SQLQuery1.SQL.Clear;
  SQlQuery1.Sql.Add('SELECT * FROM tbTeilnehmerDuB');
  SQLQuery1.Open;

  for i := 0 to SQLQuery1.Fields.Count - 1 do begin
    dbFeldName := SQLQuery1.Fields[i].FieldName;
  end;
...


Danke!!

Bianca

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: So 23.01.05 15:26 
hallo bianca!

neben der eigenschaft 'FieldName' verfügt TField auch noch über die eigenschaften
'DataType'. dort findest du die verschiedenen feldtypen der einzelnen tabellenfelder abgelegt.

um allerdings die feldlänge zu ermitteln, hab ich die eigenschaft 'FieldDefs' verwendet. mit ihrer hilfe ist es mir dann gelungen

nachdem ein beispiel mehr als 1000 worte sagt -->

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
procedure TReparaturDlg.ShowTabInfo(aFilename: TFilename);
var
  old_tbl: TTable;
  i, aRow: ShortInt;
  mFeldType: TFieldType;
begin
  old_tbl:= TTable.Create(nil);
  try
    with old_tbl do begin
      Databasename:= ExtractFilePath(aFilename);
      Tablename   := ExtractFilename(aFilename);
    end;

    old_tbl.Open;
    GridDeleteGrid(Grid1);

    for i:= 0 to pred(old_tbl.FieldDefs.Count do begin 
      GridAppendRow(Grid1);
      aRow:= i+1;
      Grid1.Cells[0,aRow]:= IntToStr(aRow);
      Grid1.Cells[1,aRow]:= old_tbl.FieldDefs[i].Name;

      mFeldType:= old_tbl.FieldDefs[i].DataType;
      case mFeldType of
        ftString:   Grid1.Cells[2,aRow]:= 'String';
        ftSmallInt: Grid1.Cells[2,aRow]:= 'SmallInt';
        ftInteger:  Grid1.Cells[2,aRow]:= 'Integer';
        ftFloat:    Grid1.Cells[2,aRow]:= 'Float';
        ftDate:     Grid1.Cells[2,aRow]:= 'Date';
        ftTime:     Grid1.Cells[2,aRow]:= 'Time';
        ftDateTime: Grid1.Cells[2,aRow]:= 'DateTime';
        ftBoolean:  Grid1.Cells[2,aRow]:= 'Boolean';
        ftAutoInc:  Grid1.Cells[2,aRow]:= 'AutoInc'
      end;

      Grid1.Cells[3,aRow]:= IntToStr(old_tbl.FieldDefs[i].Size);
      Grid1.Cells[4,aRow]:= IntToStr(old_tbl.FieldDefs[i].Precision)
    end;

    old_tbl.Close;
    GridDeleteRow(Grid1,Grid1.Row);
    Grid1.Row:= 1
  finally
    old_tbl.Free
  end;
end;


mfg, stefan
Flash106 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 234


D7 Ent
BeitragVerfasst: So 23.01.05 16:23 
alles klar danke hat funktioniert!!!

=)
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: So 23.01.05 17:34 
bitte, bitte, gern geschehen.
und danke fürs positive feedback ;)

mfg, stefan