用Createtable 建立一个DBF表格(100分)

  • 主题发起人 主题发起人 li_yu
  • 开始时间 开始时间
L

li_yu

Unregistered / Unconfirmed
GUEST, unregistred user!
用Createtable 建立一个DBF表格
 
参考一下吧, 我用的是DBISAM, DBF的和它类似.
procedure TQueue.CreateQueueFile(const aName, aPath: String);
var
TableToCreate: TDBISAMTable;
begin
TableToCreate := TDBISAMTable.Create(nil);
try
begin
with TableToCreate do
begin
DatabaseName := aPath;
TableName := aName;
Exclusive := True; //独占方式

//创建字段
with FieldDefs do
begin
Clear;
// 名称 类型 长度 不许为空
Add('RecordNo', ftAutoInc, 0, True);
Add('Session', ftInteger, 0, True);
Add('Transaction', ftString, 1, True);
Add('Priority', ftSmallint, 0, True);
Add('TimeToLive', ftInteger, 0, True);
Add('DataField', ftBlob, 0, True);
end;

//创建索引
with IndexDefs do
begin
Clear;
Add('', 'RecordNo;Session', [ixPrimary, ixUnique]);
end;

//创建表
if not Exists then
begin
CreateTable;

with RestructureFieldDefs do
begin
Clear;
Add('RecordNo', ftAutoInc, 0, True, '', '', '', '', fcNoChange, 1);
Add('Session', ftInteger, 0, True, '', '', '', '', fcNoChange, 2);
Add('Transaction', ftString, 1, True, '', '', '', '', fcNoChange, 3);
Add('Priority', ftSmallint, 0, True, '', '', '', '', fcNoChange, 4);
Add('TimeToLive', ftInteger, 0, True, '', '', '', '', fcNoChange, 5);
Add('DataField', ftBlob, 0, True, '', '', '', '', fcNoChange, 6);
end;

with RestructureIndexDefs do
begin
Clear;
Add('', 'RecordNo;Session', [ixPrimary, ixUnique], icNone);
end;

RestructureTable(0, 0, 1, 0, False, '', '队列文件', 512, -1, True);
end;
end;
end;
finally
TableToCreate.Free;
end;
end;
 
CV:
一个小程序:
...
with table2 do begin
Active:=false;
TableName:='Normal'; //表格名
TableType:=ttDBase; //数据库类型
with FieldDefs do
begin
Clear;
Add('BH',ftString,15,False);
Add('LMD',ftString,1,False);
Add('QD',ftString,1,False);
Add('NY',ftString,1,False);
Add('ZG',ftString,1,False);
Add('RIQI',ftString,8,False);
end;
CreateTable; {创建表格}
...

注意:
可能这样生成的*.DBF文件用FoxBase 之类的打不开,
参见:http://www.delphibbs.com/delphibbs/dispq.asp?lid=98659
 
后退
顶部