数学建模社区-数学中国

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

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

用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的 9 O$ m5 R" m: i$ x- q: ^2 CDelete $ V a! f6 a8 {, G, d$ iMethod并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox / Y; @1 N! i4 i 表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。 2 x c! m. |2 M5 M: q- K 9 q0 Q9 `6 H3 R+ U0 }) e//This function Pack the Paradox table. write by zodiac 6 l' K! N1 D- m- ?+ [, C3 y void __fastcall TForm1:ackParadoxTable(hDBIDb hDB, AnsiString TblName) 6 x3 X; N1 a5 o8 L3 c; i% k { 1 q0 A/ O2 O1 D& o0 ]* S //Paradox table use a quite different way to be packed than ! w4 A/ s. Z. Z. r* T //DBase or Foxpro table, it use the DBiDoRestructure not the ; Z& a3 N& u6 c; S& ?) I' Z- P/ x& A // DBiPackTable * f5 ]* w+ R0 a DBIResult rslt; : y* C' Q" ?0 j# ], O( w CRTblDesc TblDesc; " Y) A {5 ^+ ^4 F //filled the structure CRTbiDesc with 0 ' \5 [1 C0 ~, G Y memset((void *)&TblDesc,0,sizeof(CRTblDesc)); 7 k+ u+ ]% E" x; q% p, ^' @% ] [- a" w //copy the table name and type to the structure 7 ~# V: o$ T c1 Z5 V2 x# u lstrcpy(TblDesc.szTblName,TblName.c_str()); + @! u! A6 }4 s# J) n lstrcpy(TblDesc.szTblType,szPARADOX); 9 F5 V) ?0 _$ \, t- Z4 Y; l% z //set bPack to true to specify Pack Function ; E% E. b0 r- D TblDesc.bPack=true; 4 G5 b9 C/ `9 y9 ]$ n //Pack the table 1 q% `+ k5 _' G+ U' W' \( m rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); " L/ a/ f3 W( P1 Z, Y* H* M if(rslt!=DBIERR_NONE) 4 i2 L. {% n) i1 n6 H0 z& ?' ]8 ^ Application->MessageBox("不能压缩表", 5 {' Y$ A$ [8 F6 m$ r( y: v/ h" q "压缩数据表出错",MB_ICONERROR); ; M' y( e8 y0 f6 j0 c% Z+ ~8 N1 k } 6 L1 @: W6 j( B; G/ f# k/ ]注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable. 3 C, n# B& k* g3 ]& c- O : A6 c1 j/ X1 }4 D# Evoid __fastcall TForm1:ackTable(AnsiString table_name) H, z3 B! \4 ^; _) h { 3 e3 T4 V/ P9 p* z //Pack the table # {- I/ W5 p4 l. K5 ]. q TTable *temp_table=new TTable(Form1); . R: ^. Q9 O$ U( P+ \- K temp_table->DatabaseName="YourDatabaseAlias"; # b7 [7 f( d K. n9 } A' w temp_table->TableName=table_name; : K, j5 Q. K2 M/ c; Y temp_table->Exclusive=true; 5 g5 p V n* i4 O* }- Z, A) q temp_table->Open(); + z: T9 B' i3 K, h //get the Database Handle $ C0 N* T0 |1 L. j% \ hDBIDb hDB=temp_table->DBHandle; , ]3 g& ^4 E O1 E' r. K temp_table->Close(); 4 T2 `, \ s2 @8 a' t g, H ?! z. H( S& J1 P& t. c PackParadoxTable(hDB,table_name); * H! A: ], E- X3 u- `# K 5 K8 [2 p: M/ F% ?) U2 {. | temp_table->Close(); # P, ?2 F( X I0 V9 N6 b6 q1 L% A temp_table->Free(); 6 E1 p$ H5 k/ T# v* ^" Z$ g0 h } 5 U& p* A" l- F/ c: _ 7 {9 ]7 l9 Z4 Q( q9 _1 e" V7 O # I, X* G7 A3 D对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。






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