uses BDE;...procedure TForm1.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 not Table.Active then
raise EDatabaseError.Create('Table must be opened to pack');
if not Table.Exclusive 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'); Table.Open;
end;/*注意TTable必须是独占方式,即:Exclusive 设为 True*/
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
begin
// Make sure the table is open exclusively so we can get the db handle...
if not Table.Active then
raise EDatabaseError.Create('Table must be opened to pack');
if not Table.Exclusive 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'); Table.Open;
end;/*注意TTable必须是独占方式,即:Exclusive 设为 True*/
Table1.Close;
Table1.Exclusive := true;
Table1.Open;
Table1.Delete;
PackTable(Table1);
Table1.Close;
Table1.Exclusive := false;
var
errResult: DBIResult;
begin
if not table1.Exclusive then
begin
result:=false;
showmessage('false !');
exit;
end;
errResult:=DbiPackTable(Table1.DBHandle, Table1.Handle, nil, szdbase, True);
if errResult=dbierr_none then
begin
result:=true;
showmessage('ok !');
end
else
begin
result:=false;
showmessage('false !');
end;end;procedure TForm1.dClick(Sender: TObject);
begin
Table1.Delete;
Table1.Close;
Table1.Exclusive := true;
PackdbfTable(Table1);
Table1.Exclusive := false;
table1.open;
end;结果是:False !
通过单步跟踪发现,前面的代码均已通过,出现的 “ False ” 是第二个False,也就是在执行if errResult=dbierr_none then 时出现的,请帮助分析一下 !
procedure TForm1.dClick(Sender: TObject);
begin
Table1.Delete;
Table1.Close;
Table1.Exclusive := true;
Table1.open; //添加
PackdbfTable(Table1);
Table1.Close; //添加
Table1.Exclusive := false;
table1.open;
end; //Table应该在open状态,函数才起作用。 要不,您在packdbftable()中添加:
//最前面
if not table.Active then
try
table.open;
except
result := false;
end;
...
//最后面
table.Close;
========================
试试,祝好运!
我已经试过了,如果在 PackdbfTable(Table1); 前面加上 Table1.open; 出现错误提示: Table is busy ! (此时并没有其他程序访问该表)。
begin
with table1 do
begin
delete;
close;
exclusive:=true; // 独占
// 这中间少了个打开库的语句
dbipacktable(table1.dbhandle,table1.handle,nil,nil,true);
exclusive:=false;
open;
end;
end;pack之前少了个打开的语句,再试!
我也试过,成功地pack掉
将“open;”去掉后不再出现 busy 的提示,但是最后用Foxpro打开表看,还是只加了个删除记录而已,依然没有彻底删除记录,是不是对Ttable控件的属性有什么要求,你能给我一个你测试通过例子吗,谢谢了 !
procedure TForm1.deleteClick(Sender: TObject);
begin
with table1 do
begin
delete;
close;
exclusive:=true; // 独占
** dbipacktable(table1.dbhandle,table1.handle,nil,nil,true);
exclusive:=false;
open;
end;
end;如果将带**的代码改为check(dbipacktable(table1.dbhandle,table1.handle,nil,nil,true));
出现错误提示:Invalid Handle !请问是什么原因,问题是否出在 table 呢 ??
请问英雄能否给小弟一个简单地例子,谢谢啦 !!!
然后,添加几条记录。
编写程序时,把TTable的exclusive设为true(不要动态设置)
再后,用我上面的函数及调用方法,试着删除看。--------------------我想,你一定在其他地方打开了表,又没有关闭。
你在设计的时候把TTable的exclusive设为true,然后设Active为true,
看看是否出现busy,不出,说明在程序运行中,打开了表,没有关闭,
然后就做了你上面的操作。如果您确认程序没有问题,试着编译完程序后,关机->开机->运行您的程序
->删除记录->关闭退出您的程序->看看是否真的删除。------------------
对于兄台的帮助,小弟非常非常感激,谢谢 !