|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的 0 ?: C: h, T9 M
Delete
5 a' u7 O( Y$ _; TMethod并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox 4 t5 ^6 ]' S: a7 \/ c
表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。 . C3 o7 _$ Q$ K5 N
3 u, C6 W' g; k* T
//This function Pack the Paradox table. write by zodiac ' }; W, J! p. l0 a5 V
void __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName) , O2 |( _( ?2 n& ~+ D0 S
{ 3 Y7 p# \( Q1 A' m' [1 i
//Paradox table use a quite different way to be packed than
. _/ i2 k) a4 v3 B, { ~9 M! o0 Y //DBase or Foxpro table, it use the DBiDoRestructure not the
( ?& P1 B' T& C4 L // DBiPackTable
5 e) M2 |. `( I8 b DBIResult rslt;
, R! W/ t4 W1 S1 I8 r: X5 w CRTblDesc TblDesc;
9 ?% W; U* t0 p1 N! {7 `/ v* } //filled the structure CRTbiDesc with 0 ( X- h- k. [' k
memset((void *)&TblDesc,0,sizeof(CRTblDesc));
1 l/ R5 _* F% _4 w0 S2 w& ^7 u //copy the table name and type to the structure
2 v6 N/ Z7 c0 \% ^0 E3 C lstrcpy(TblDesc.szTblName,TblName.c_str());
7 R2 {( |. ], s7 e3 h$ h lstrcpy(TblDesc.szTblType,szPARADOX);
- C: w* |/ ~. W3 ]- y; \4 b //set bPack to true to specify Pack Function " G. t3 O# X& X8 L, |- j
TblDesc.bPack=true; % K' ?* A8 c7 X* S. {
//Pack the table * W$ m+ d0 h3 H7 {5 W" |
rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false);
. z! p6 g2 c+ }& O if(rslt!=DBIERR_NONE)
F1 A% U( n B/ j Application->MessageBox("不能压缩表", ! y' }% s' P( l
"压缩数据表出错",MB_ICONERROR);
! h- U. O9 e% p$ D4 f6 g5 c}
9 _& `0 _8 a2 R4 `* M# X9 f注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable. % R/ }7 j- a4 a
5 n# j T% g! i) m/ Qvoid __fastcall TForm1: ackTable(AnsiString table_name) 6 m7 m# k J3 V. u% K
{ . A4 R& X; n! k2 w- h9 H" d% a0 F+ g
//Pack the table 1 x8 [1 ^0 U+ [; ^, }
TTable *temp_table=new TTable(Form1); / K8 h) c+ h7 m, p5 Q. v! M
temp_table->DatabaseName="YourDatabaseAlias"; 2 j; g! ]& C& D, o' |" ]
temp_table->TableName=table_name; 3 i0 Z2 h5 D5 o; ~0 f: N
temp_table->Exclusive=true; ) I" w; W% J. s& X! N1 A
temp_table->Open(); + }3 p" \2 k* }4 L1 i& I5 `, V: p
//get the Database Handle
: m( O' [6 h' }3 C/ a hDBIDb hDB=temp_table->DBHandle; 1 G- z/ j( U# Q3 T8 W. a
temp_table->Close(); ; u# f7 D! Q3 p2 S
, ^( P- ]2 U. X* {2 [
PackParadoxTable(hDB,table_name);
" U! B+ v& v3 v1 z2 f) y
* J9 ~7 @* ~/ G temp_table->Close();
! I. T1 a- _- t! ]: ]* ]4 } temp_table->Free();
6 W+ J$ ?: Q+ N}
4 ~' s6 n# c6 n9 _; S3 F0 l" o : C, L- z; {9 p4 f x
% f, c- X: E1 E5 {3 w对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |