|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的
) D% X4 f* I+ B$ bDelete
) v! }) a$ Z& {- KMethod并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox
3 {/ P4 s' W: r+ O+ ^( j表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。 5 N6 A' `( m2 e% L i
6 Q9 O1 L" \6 G9 D* b//This function Pack the Paradox table. write by zodiac $ M+ f& s7 r7 U' Z! B5 M& W: d
void __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName)
. q. v' m9 v7 W0 s5 N6 N5 K1 K{ , l- |# R( x3 N' x
//Paradox table use a quite different way to be packed than 1 V' m3 S# ~/ S2 T! }
//DBase or Foxpro table, it use the DBiDoRestructure not the
% m( K4 [' u* y // DBiPackTable
6 Z' C% }; {5 h# j1 V, z DBIResult rslt; & {2 X4 z" e( D* M9 B
CRTblDesc TblDesc; 8 L( W3 D3 b3 ~
//filled the structure CRTbiDesc with 0 5 G! T; x$ q9 D! Q! E% N8 q3 r
memset((void *)&TblDesc,0,sizeof(CRTblDesc));
+ I9 S! } a- o" p0 |' W //copy the table name and type to the structure
7 \( y' y V. D+ R. P. E lstrcpy(TblDesc.szTblName,TblName.c_str());
. Z; A; n. o% L* A6 _- ] lstrcpy(TblDesc.szTblType,szPARADOX); & B4 G8 y( u7 i( ~6 `: i1 I3 _
//set bPack to true to specify Pack Function + ?# Z z5 t, S) m6 E
TblDesc.bPack=true; L6 I+ q; g9 [4 p+ U
//Pack the table
; ^$ R7 [1 d7 K6 d6 f1 p$ V rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false); ; U3 c# v( l/ k9 _ l8 ?
if(rslt!=DBIERR_NONE) ) A3 r0 d6 \: L# ]" ?* L$ f5 M! S; E
Application->MessageBox("不能压缩表", " }- d8 N. X3 f; I0 H
"压缩数据表出错",MB_ICONERROR); " G$ n' z) {0 C; ]0 O
}
0 a7 O" W" m" W+ h% p注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable.
* W6 [/ l) {5 R" }; ]( e 6 P3 v8 g+ E; p- ~. U7 ^6 |; W! F
void __fastcall TForm1: ackTable(AnsiString table_name) % O+ X- n! C8 o8 _$ s0 D7 X' ^, H
{
/ e5 I) ]4 E; F# P W9 w$ l //Pack the table 5 V7 P: O* U# G7 K1 D J
TTable *temp_table=new TTable(Form1);
$ [" ^+ o! v. j, D) I9 m temp_table->DatabaseName="YourDatabaseAlias"; 2 T% V( \' C3 o+ F3 X+ w( K" U
temp_table->TableName=table_name; 9 ~$ x5 E8 |" h$ b9 F# H, Q
temp_table->Exclusive=true; - y) T6 z9 y1 L
temp_table->Open(); - `& o3 R6 k. u: M! ?' n$ M
//get the Database Handle * w9 x2 ~7 F# |: z9 J! @
hDBIDb hDB=temp_table->DBHandle; : B: [ A* e U$ k+ U }7 [
temp_table->Close();
6 ]" b, U; m* Y' x9 h: W/ S# n. u" g h3 Q. Y. h* f; v1 |
PackParadoxTable(hDB,table_name); 5 L) L' f0 y: {6 Z
' `& r8 f1 U- @
temp_table->Close(); 3 V, Z- I, o) R5 |& R5 F- c
temp_table->Free(); ( O; t- v( K2 h
} ' z$ x) o; b) v) m$ \
' l5 ?* ?7 \) Q. [2 j
$ b1 z8 J6 `9 g7 S对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |