|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的
5 s8 o6 p4 p* yDelete
. q+ {5 h8 Q/ g) qMethod并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox
0 r T# N% j+ N/ O表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。
. _$ T0 g; b' v5 ]" P $ I) { z. C: ~1 j, k: A) G
//This function Pack the Paradox table. write by zodiac 4 v7 ]! H7 R. w0 e" ~" {$ Y# R4 p
void __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName) 5 W; ~) r& L" ]+ k9 \9 ~# j
{
/ f W2 z# e, e/ K //Paradox table use a quite different way to be packed than ; H4 a: e$ i( O3 P# g
//DBase or Foxpro table, it use the DBiDoRestructure not the
, S" x8 Z+ H6 V5 b1 [ // DBiPackTable
5 C, X/ a2 {6 L DBIResult rslt; ' y: S" B- S7 A0 K
CRTblDesc TblDesc;
; f; B8 d- C( c: h& L2 H //filled the structure CRTbiDesc with 0 + g$ Q1 l) S4 h; p- w
memset((void *)&TblDesc,0,sizeof(CRTblDesc));
3 m- V. D% l P1 P7 v //copy the table name and type to the structure 3 R% P( r# R. U- f; W( t$ d
lstrcpy(TblDesc.szTblName,TblName.c_str());
+ `; A- B" y1 Q2 X- W/ H8 E lstrcpy(TblDesc.szTblType,szPARADOX);
l6 e( Y# R- y9 Q1 g //set bPack to true to specify Pack Function
* y8 a Z4 v9 B- w- R TblDesc.bPack=true;
. O2 k7 Q1 S0 ~ //Pack the table % X$ ?3 e+ S# I
rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); 7 Z5 m5 C; H+ X( c0 U
if(rslt!=DBIERR_NONE) * R8 S% {5 p4 L) n
Application->MessageBox("不能压缩表", # w1 `' K) x H
"压缩数据表出错",MB_ICONERROR);
% `7 L0 `+ y) D( i}
* ~6 L5 V6 G1 V# Q: C+ S! ^注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable. * \- A p( D9 { Y- a' y
: ~0 ~, ^7 V9 Y$ x* c+ X& Z+ t5 h" I
void __fastcall TForm1: ackTable(AnsiString table_name) 1 X7 n4 e2 x* K1 i) P& M& Z
{ + q- H6 o. y9 d9 P
//Pack the table
. B& Y! ]' j: }' f3 n2 a TTable *temp_table=new TTable(Form1); 5 z* ~% J: b, A3 @8 r' V& G4 w
temp_table->DatabaseName="YourDatabaseAlias";
" L! n' k& m2 `9 J7 A2 V2 y7 \ c! D% ` temp_table->TableName=table_name;
+ {; n- V. d" w3 J8 Z temp_table->Exclusive=true;
/ Y* {+ B. `# G& o1 T temp_table->Open();
6 H/ C! G( J3 g% p" @8 `2 ` //get the Database Handle / y( J8 a/ H8 d( ~; E0 d
hDBIDb hDB=temp_table->DBHandle; 2 @+ e# r" _% U' w4 Z6 p5 X
temp_table->Close();
+ _) S: i i, y5 S. D
5 B* ?3 K: J5 W$ A( X5 i2 N% s' X PackParadoxTable(hDB,table_name);
8 t" w; C+ {2 C
9 T) f6 ^% z9 m8 ` temp_table->Close(); / g, J- j @/ [; Q
temp_table->Free();
' w3 Q; h, V4 l6 d}
: P/ u, J3 \, I v: O( T $ Z. @- w* Q9 h1 \" d3 Y! t; A, f
) E- {/ a, T% o" {2 @5 ]" u( d$ W
对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |