数学建模社区-数学中国

标题: 如何给Table减肥 [打印本页]

作者: 韩冰    时间: 2005-1-26 12:36
标题: 如何给Table减肥

用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的 `8 K" f5 N4 YDelete % D& m# Q! P" n" H) m+ H Method并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox / z9 f% ? y r9 @( h表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。 $ {% s1 ~' n/ h# y - C7 T) B5 w2 a, m; a //This function Pack the Paradox table. write by zodiac 2 j; }" ]' }$ n0 M% U void __fastcall TForm1:ackParadoxTable(hDBIDb hDB, AnsiString TblName) + r4 |4 p' @9 O$ n+ C4 c{ ( y. [' u- N+ M- j //Paradox table use a quite different way to be packed than $ e% W8 j4 L3 {: _% J //DBase or Foxpro table, it use the DBiDoRestructure not the / G5 p2 X2 B; x; n // DBiPackTable ( U1 R9 w5 @5 x4 n. }6 b3 h: S DBIResult rslt; + G$ |1 h; C9 v% l CRTblDesc TblDesc; - f0 C7 l+ v5 }- y4 p: Q8 C //filled the structure CRTbiDesc with 0 3 o/ } I9 A$ }/ [) v" B, E2 I3 _ memset((void *)&TblDesc,0,sizeof(CRTblDesc)); # i3 ~4 @, F5 o7 J6 { //copy the table name and type to the structure : S9 l1 Y, H3 Q0 ?6 N- l lstrcpy(TblDesc.szTblName,TblName.c_str()); % T5 w' L7 q/ I2 g y lstrcpy(TblDesc.szTblType,szPARADOX); 7 t$ W8 _, j6 {- h //set bPack to true to specify Pack Function 0 l; d; X2 |; c$ ] TblDesc.bPack=true; V* O$ p7 l* y& S$ \ //Pack the table " Y! C* m. j1 V0 E$ F rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); - Y, d b5 Q+ O" _- S7 m2 ~- l, k+ b if(rslt!=DBIERR_NONE) : p8 W: }3 c3 d- i) _ Application->MessageBox("不能压缩表", & K4 G4 ]/ Y. L l7 J, e "压缩数据表出错",MB_ICONERROR); # s# ?, G9 ]0 v/ z# s+ Z} 9 V' ]: T4 ]9 E7 v/ {2 {注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable. 7 k: M! [2 O* m1 c) A N1 C/ t ! m; ~ l0 O4 W2 `- i5 i0 u* z+ x void __fastcall TForm1:ackTable(AnsiString table_name) # d2 l+ q% e8 f/ U9 Y w( b{ + F0 U( r, _; T& [% ~& N //Pack the table ) k8 D1 H& t% b2 ~ TTable *temp_table=new TTable(Form1); - R1 o( z+ b6 h3 K; `% F% x% E temp_table->DatabaseName="YourDatabaseAlias"; 4 C+ t" _6 R: F! Z& m# ~* g9 d7 B1 m O temp_table->TableName=table_name; 4 e0 t) x7 ~7 d9 f4 V! G+ p temp_table->Exclusive=true; 1 g9 B+ \' l+ b: h2 n temp_table->Open(); ( @5 E$ ?2 i# Z- c5 F //get the Database Handle ) K* F2 Y8 m# [# ^/ k( _ hDBIDb hDB=temp_table->DBHandle; 4 W! j1 t% m3 V0 d/ F0 T6 m' U( c temp_table->Close(); 1 c6 N! D5 B4 Q @ & e: j l# D. n PackParadoxTable(hDB,table_name); & t' O p: R! L ! H2 B& M2 P8 ^: a! Y# I/ Q, n. K temp_table->Close(); ( G, `* u* }! m6 d$ s+ x temp_table->Free(); 4 ], [. L0 u2 s* u4 E } # t. u2 v6 b' P% }! y 9 m3 e) k7 z5 R M$ A6 p7 ~! v . Q, l2 \0 ]9 @ S) H 对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5