|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的
# [; K( v$ ], G0 `" a4 x, ~+ {Delete : \5 r0 Y& u8 b. q
Method并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox
$ g2 o6 i# q, `) H! M表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。 8 j+ `6 a- K" c6 s
3 P! X+ C* @/ j" k//This function Pack the Paradox table. write by zodiac
* |$ J6 c" \/ `void __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName) 2 T5 e9 [& j, F$ z# U" z$ l+ z
{ ( s8 U) G+ x5 Y& b$ ~8 }
//Paradox table use a quite different way to be packed than 7 x$ N; U8 `$ G; o
//DBase or Foxpro table, it use the DBiDoRestructure not the # y6 a4 w2 e7 ~) @4 _+ s: ~
// DBiPackTable
1 K1 x x# o0 f- e8 M& ^( Z" | DBIResult rslt; 4 d, ~) Z9 b3 |3 \8 Z. K/ S
CRTblDesc TblDesc;
) P2 G" C+ ~7 J* ~; f, s" q //filled the structure CRTbiDesc with 0 % G* I/ ~- M; n: L8 c# A
memset((void *)&TblDesc,0,sizeof(CRTblDesc));
; o" e4 }; U1 N0 l //copy the table name and type to the structure . K2 Y6 o! `& u9 \& L1 v
lstrcpy(TblDesc.szTblName,TblName.c_str());
! @# X4 j- o. ~: y9 M& W lstrcpy(TblDesc.szTblType,szPARADOX);
4 e# C3 P5 n% S1 o. i9 P //set bPack to true to specify Pack Function
+ V$ {/ [0 }: S! E% c3 i# C TblDesc.bPack=true; # u7 x9 n; d% L0 U+ e
//Pack the table
; Y/ @) d4 q" g3 R rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); # U" B2 P" F; s" U1 t2 ^
if(rslt!=DBIERR_NONE) $ M( w& i, O m) i
Application->MessageBox("不能压缩表", ' a9 {7 I# f8 r7 I
"压缩数据表出错",MB_ICONERROR);
1 k' u0 W+ W/ m! h} ) s7 p! T4 F( L! ]! o1 K
注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable.
$ J/ l/ |# z# i- M. |
( z2 e% R; a7 |8 t) y# V% Mvoid __fastcall TForm1: ackTable(AnsiString table_name) 5 r& ]: }6 A ?$ t: s
{ # k' f% @1 u' x& ?! ?( K. a
//Pack the table
& M {) T9 u( @! Z+ g8 } s: F2 K TTable *temp_table=new TTable(Form1); % P p' v U: K! J; I z
temp_table->DatabaseName="YourDatabaseAlias";
4 G5 T2 N$ v8 F+ c6 A5 |; H& b temp_table->TableName=table_name; ) Z* m; g- x' U, T: n# k: e: E
temp_table->Exclusive=true; ; y2 R, V2 F& Q
temp_table->Open();
& O2 x; ?. ?9 k8 }3 U/ _ //get the Database Handle : H2 i& o! j; X$ Q9 u# z9 @
hDBIDb hDB=temp_table->DBHandle; ' z r8 q: g! j: j- s1 ?$ m
temp_table->Close(); 6 {- C" F( h0 E- P2 u) N
3 F- n7 p5 T# C
PackParadoxTable(hDB,table_name);
3 c( _1 \: h( a
7 d, o# V# R) U) u0 G temp_table->Close();
* J( W! k: H& p& Q, W6 l. N temp_table->Free();
, t. F2 y9 q z# ?) F" k* z} ; E5 |+ ^* n$ \
. C; t* m% n4 I* p ( c( z' ?2 W; R
对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |