TTable.Delete() 僅標記刪除旗標,不釋放磁盤空間。反覆增刪導致表文件膨脹。
DBase/FoxPro 可用 PACK 命令;Paradox 需 DbiDoRestructure;DBF 用 DbiPackTable。
void __fastcall PackParadoxTable(hDBIDb hDB, AnsiString TblName) {
DBIResult rslt;
CRTblDesc TblDesc;
memset(&TblDesc, 0, sizeof(TblDesc));
lstrcpy(TblDesc.szTblName, TblName.c_str());
lstrcpy(TblDesc.szTblType, szPARADOX);
TblDesc.bPack = true; // 指定打包操作
rslt = DbiDoRestructure(hDB, 1, &TblDesc, NULL, NULL, NULL, false);
if (rslt != DBIERR_NONE)
Application->MessageBox("不能壓縮表", "錯誤", MB_ICONERROR);
}
調用前必須關閉表:
void __fastcall PackTable(AnsiString table_name) {
TTable* temp = new TTable(Form1);
temp->DatabaseName = "YourDatabaseAlias";
temp->TableName = table_name;
temp->Exclusive = true; // 獨佔模式
temp->Open();
hDBIDb hDB = temp->DBHandle;
temp->Close(); // 關閉後方可重組
PackParadoxTable(hDB, table_name);
delete temp;
}
table->Close();
// 獨佔打開
for (;;) try { table->Exclusive = true; table->Open(); break; } catch (...) {}
if (DbiPackTable(table->DBHandle, table->Handle, NULL, szDBASE, true) != DBIERR_NONE)
Application->MessageBox("不能刪除記錄", "錯誤", MB_ICONSTOP);
Exclusive=true)。DbiPackTable、DbiDoRestructure 詳細參數說明。