高手请进

hcman 2000-01-27 03:45:00
请问如何获得一个表(fox数据库)的字段名,字段类型,字段长度,及小数位的数据
...全文
189 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tommy Chang 2000-02-02
  • 打赏
  • 举报
回复
这个问题只需用session(当你的form上有database类控件时,delphi会给你一个默认的session对象)就可以了。我上面的例子不过是个较完整的数据库浏览程序的源码而已。
barton 2000-02-02
  • 打赏
  • 举报
回复
RX控件不是很简单吗?实在不行研究一下RX的源码。
laotan 2000-01-31
  • 打赏
  • 举报
回复
请看“table中字段属性的运用问题”问题中Laotan给予的回答
Tommy Chang 2000-01-31
  • 打赏
  • 举报
回复
其实用delphi来动态找字段属性等非常方便,但找外健、主键就有麻烦。
以下是我自用的一个数据浏览,用来简单输入数据、执行sql的小东西,比SQL Explorer 功能差很多,但是足以回答这位朋友的问题

以下是源程序
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, StdCtrls, Db, DBTables, ExtCtrls, DBCtrls, Buttons;

type
TForm1 = class(TForm)
DataSource1: TDataSource;
Table1: TTable;
ListBox1: TListBox;
ListBox2: TListBox;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DBNavigator1: TDBNavigator;
DBNavigator2: TDBNavigator;
DataSource2: TDataSource;
Query1: TQuery;
Memo1: TMemo;
SpeedButton1: TSpeedButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox2DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
session.GetDatabaseNames(listbox1.Items);
with stringgrid1 do
begin
cells[0,0]:='Field';
cells[0,6]:='Atr Set';
cells[0,2]:='DataSize';
cells[0,3]:='DataType';
cells[0,4]:='Nullable';
cells[0,5]:='Modifiable';
cells[0,1]:='Fullname';
end;
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
var
i:integer;
begin
if session.Active then
session.Close;
for i:=0 to listbox1.items.count-1 do
begin
if listbox1.Selected[i]
then
begin
session.GetTableNames(listbox1.items.Strings[i],
'*.*',true,true,listbox2.items);
if table1.Active then table1.close;
if query1.Active then query1.close;
query1.DatabaseName:=listbox1.Items.strings[i];
table1.DatabaseName:=listbox1.items.Strings[i];
end;
end;
end;

procedure TForm1.ListBox2DblClick(Sender: TObject);
var
i,j:integer;
begin
with stringgrid1,table1 do
for j:=0 to listbox2.Items.Count-1 do
begin
begin
if listbox2.Selected[j] then
begin
if Active then close;
TableName:=listbox2.items.strings[j];
open;
end;
end;
ColCount:=FieldList.Count+1;
for i:= 0 to FieldList.Count-1 do
begin
Cells[i+1,0]:=FieldList.Fields[i].FieldName;
Cells[i+1,6]:=FieldList.Fields[i].AttributeSet;
cells[i+1,2]:=inttostr(FieldList.Fields[i].datasize);
case FieldList.Fields[i].DataType of
ftUnknown:cells[i+1,3]:='Unknow';
ftString:cells[i+1,3]:='String';
ftSmallint:cells[i+1,3]:='SmallInt';
ftInteger:cells[i+1,3]:='Integer';
ftWord:cells[i+1,3]:='Word';
ftBoolean:cells[i+1,3]:='Boolean';
ftFloat:cells[i+1,3]:='Flaot';
ftCurrency:cells[i+1,3]:='Currency';
ftBCD:cells[i+1,3]:='BCD';
ftDate:cells[i+1,3]:='Date';
ftTime:cells[i+1,3]:='Time';
ftDateTime:cells[i+1,3]:='DateTime';
ftBytes:cells[i+1,3]:='Byte';
ftVarBytes:cells[i+1,3]:='VarBytes';
ftAutoInc:cells[i+1,3]:='AutoInc';
ftBlob:cells[i+1,3]:='Blob';
ftMemo:cells[i+1,3]:='Memo';
ftGraphic:cells[i+1,3]:='Graphic';
ftFmtMemo:cells[i+1,3]:='FmtMemo';
ftParadoxOle:cells[i+1,3]:='ParadoxOle';
ftDBaseOle:cells[i+1,3]:='DBaseOle';
ftTypedBinary:cells[i+1,3]:='TypeBin';
ftCursor:cells[i+1,3]:='Cursor';
ftFixedChar:cells[i+1,3]:='Fixedchar';
ftWideString:cells[i+1,3]:='WideString';
ftLargeint:cells[i+1,3]:='Largeint';
ftADT:cells[i+1,3]:='ADT';
ftArray:cells[i+1,3]:='Array';
ftReference:cells[i+1,3]:='Reference';
ftDataSet:cells[i+1,3]:='DataSet';
else
cells[i+1,3]:='UnknowType';
end;
if fieldlist.Fields[i].Required then
Cells[i+1,4]:='No'
else
cells[i+1,4]:='Yes';
if fieldlist.Fields[i].CanModify then
cells[i+1,5]:='Yes'
else
cells[i+1,5]:='No';
cells[i+1,1]:=fieldlist.Fields[i].FullName;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if query1.Active then query1.close;
query1.SQL:=memo1.Lines;
query1.open;
end;

end.
光明山人 2000-01-27
  • 打赏
  • 举报
回复
低手也进来了,:)

我指的是运行时编程的方法,且需要用TTable。
将TTable的DatabaseName和TableName填好后,执行TTable.FieldDefs.Update(不需要打开TTable), 则该表的所有字段的属性就在TTable.FieldDefs.Items数组属性中,它们为TFieldDef类型,TFieldDef.Name为字段名,TFieldDef.DataType为字段类型,TFieldDef.Size为字段长度,TFieldDef.Precision为小数位。当然还有更多属性,你自己看看TFieldDef的帮助吧。
字段的数目为:TTable.FieldDefs.Count,注意,下标从0开始喔。

5,387

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧