如何在程序中实现对paradox表的pack?(50分)

  • 主题发起人 主题发起人 hotkey
  • 开始时间 开始时间
Try the code following ...

procedure PackTable(Table: TTable);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
begin
// Make sure the table is open exclusively so we can get the db handle...
if Table.Active = False then
raise EDatabaseError.Create('Table must be opened to pack');
if Table.Exclusive = False then
raise EDatabaseError.Create('Table must be opened exclusively to pack');

// Get the table properties to determine table type...
Check(DbiGetCursorProps(Table.Handle, Props));

// If the table is a Paradox table, you must call DbiDoRestructure...
if Props.szTableType = szPARADOX then
begin
// Blank out the structure...
FillChar(TableDesc, sizeof(TableDesc), 0);
// Get the database handle from the table's cursor handle...
Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
// Put the table name in the table descriptor...
StrPCopy(TableDesc.szTblName, Table.TableName);
// Put the table type in the table descriptor...
StrPCopy(TableDesc.szTblType, Props.szTableType);
// Set the Pack option in the table descriptor to TRUE...
TableDesc.bPack := True;
// Close the table so the restructure can complete...
Table.Close;
// Call DbiDoRestructure...
Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
end
else
// If the table is a dBASE table, simply call DbiPackTable...
if Props.szTableType = szDBASE then
Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, TRUE))
else
// Pack only works on PAradox or dBASE; nothing else...
raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +
'type to pack');
end;
 
哇,这不是Delphi的帮助文件吗?
 
参考一下下面的C++ Builder代码,这很容易改成Delphi代码的。
void dBasePack(TTable *Source, bool ReIndex)
//ReIndex: 重新生成索引
{
if (!Source->Active || !Source->Exclusive)
throw EDatabaseError("数据表必须以独占方式打开!");
try{
Check(DbiPackTable(Source->DBHandle, Source->Handle, NULL, NULL, ReIndex));}
__finally{
Source->CursorPosChanged();
Source->Refresh();
}
}
 
Delphi代码大概是这样子(注意要引用BDE单元):
procedure dBasePack(Source: TTable; ReIndex: Boolean);
begin
if not Source.Active and not Source.Exclusive then
raise EDatabaseError.Create('数据表必须以独占方式打开!');
try
Check(DbiPackTable(Source.DBHandle, Source.Handle, Nil, Nil, ReIndex));
finally
Source.CursorPosChanged;
Source.Refresh;
end;
end;
 
谢谢各位的指点!
 
后退
顶部