|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的
0 N, i C! M* Q9 _. Q. e! \Delete # @( R* R7 s f8 I$ ]. z
Method并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox + g" X$ c K+ W2 N5 l5 |$ F
表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。
/ g! [3 @, O( |# Q8 p
; \1 G4 a9 f N4 n1 ^0 B/ m9 h% {//This function Pack the Paradox table. write by zodiac / k8 \0 D3 Y; @
void __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName)
$ J: v/ k) K. e" p{
+ N/ i2 j: i) ]% v- A //Paradox table use a quite different way to be packed than
9 Q1 n3 R( K5 H" T- s; E+ M //DBase or Foxpro table, it use the DBiDoRestructure not the
6 D9 @% c5 `1 V, j% l, U // DBiPackTable
9 p# B9 [, Q9 {! L, |( m DBIResult rslt; 9 [- H$ Z0 L7 r8 n/ H
CRTblDesc TblDesc;
- B. c0 @/ }2 f, w //filled the structure CRTbiDesc with 0
, T; q0 [: T1 B) ?5 c2 L memset((void *)&TblDesc,0,sizeof(CRTblDesc)); ! W/ z: [/ o" b6 [3 P8 ^4 M
//copy the table name and type to the structure M$ r* T+ M$ E+ V; W# C. s
lstrcpy(TblDesc.szTblName,TblName.c_str()); 7 t8 M3 u9 r% H" y, N) p
lstrcpy(TblDesc.szTblType,szPARADOX); . ~' z+ O$ _( h, r2 M3 C, Y
//set bPack to true to specify Pack Function
% B! G( [2 p( ^3 g0 v' [$ N: ] TblDesc.bPack=true; " L. G: V2 z* V& ], [9 L
//Pack the table 9 q; b8 I g; o3 C
rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); 9 e( T7 w' P' x
if(rslt!=DBIERR_NONE)
V1 J p+ X5 h: U Application->MessageBox("不能压缩表", % q* M4 P; r. V" M; A9 o
"压缩数据表出错",MB_ICONERROR);
+ s9 }8 v3 K2 S% ^9 I}
0 c, }6 v: X( D2 `注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable. + }) `& c1 m9 K; r
4 }: u, e- u0 O
void __fastcall TForm1: ackTable(AnsiString table_name) 5 t! x8 ?( N3 F# D) a# B
{ 0 k, Q& w! e, M, W- f+ u
//Pack the table
9 S3 L b4 X9 c" F7 ^/ [ TTable *temp_table=new TTable(Form1); 6 M: [+ m, L- D, s( w
temp_table->DatabaseName="YourDatabaseAlias"; 7 A- l% y1 S! O& D, }
temp_table->TableName=table_name;
* z$ e* p7 {7 x/ A% h& f8 \, ^- k temp_table->Exclusive=true;
# A0 y4 S6 I9 y5 ^ o$ f& } temp_table->Open(); ! g( q) V. j" }- f7 R
//get the Database Handle
$ b. [% N8 X% x1 e+ E7 k: l hDBIDb hDB=temp_table->DBHandle; ) Z5 W1 G! o' S% x" s; [
temp_table->Close(); ( o6 P( S7 y7 _8 r; `; j$ n( O
* J1 m' K. C3 B1 _! V( d* {
PackParadoxTable(hDB,table_name); 9 ]) X# ^8 t3 D7 e3 ^8 D
3 u, |* ^, ?& v' B temp_table->Close();
6 }" F# B# Z( l2 R# M temp_table->Free();
3 t8 e; H" f* `- |, W4 y} # i- E/ z/ a, }( R* \0 O0 r
9 {1 y* [. @! U, I% s1 C. b
_- P3 J5 ^. |, E对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |