|
用BCB进行多媒体数据库开发时常会发现这样一个现象,当你把一条记录从表中删除时,表的大小并没有相应减小。这样在进行多次插入删除之后,表文件就会越来越庞大。之所以会有这种现象,是因为TTable控件的 4 a& V1 ^9 R1 o( W3 K4 x; W
Delete
# S% a5 B; `. Q& G9 B4 iMethod并不真正从表中删除记录,而只是在记录前加上一个删除标志。在DBase和Foxpro中有Pack语句对表进行压缩,但在TTable类中却没有相应的函数。其实在BDE的API函数中已经提供了DbiPackTable来对DBase或Foxpro表进行压缩,但是这个函数对Paradox的表不起作用。要想给Paradox 8 n& Q l. C* R4 ], i# }
表减肥得用DbiDoRestructure函数来完成,以下例程完成Pack Paradox表的功能。
/ Q! q" K5 w) |9 U3 I* T
" q0 [+ M4 _9 r0 w1 w* p4 ~0 s//This function Pack the Paradox table. write by zodiac
$ a. P$ T) m- n* _3 Hvoid __fastcall TForm1: ackParadoxTable(hDBIDb hDB, AnsiString TblName) ; B! M; ^/ x8 \0 D% e1 r1 L& F0 a4 \
{
* l) C( {" r9 y+ Z* y1 N //Paradox table use a quite different way to be packed than $ J0 l6 T' L" V" ?4 t- X
//DBase or Foxpro table, it use the DBiDoRestructure not the + }5 x; w `& e y
// DBiPackTable
" f& F0 ~0 v, {+ _ I% b DBIResult rslt; 7 O! T; `& z# ?" B( \. B
CRTblDesc TblDesc; + v% z9 w0 u7 I, `; Z# |
//filled the structure CRTbiDesc with 0
/ S+ `1 R3 y( W8 X5 K3 Y+ P memset((void *)&TblDesc,0,sizeof(CRTblDesc));
8 Z* ?* C2 B5 b; T! h7 `! E //copy the table name and type to the structure
( n" O" {; |4 K' R9 r. D- y: @0 w lstrcpy(TblDesc.szTblName,TblName.c_str()); 7 |: Y1 |& n" W! Y
lstrcpy(TblDesc.szTblType,szPARADOX); ' B+ I! |. \# c: D4 i
//set bPack to true to specify Pack Function - m2 ]. i3 S& g8 b: r9 v
TblDesc.bPack=true; . A) T c8 n" |9 t
//Pack the table / d w S0 U k5 R
rslt=DbiDoRestructure(hDB,1,&TblDesc,NULL,NULL,NULL,false);
6 g+ ?8 D- [0 d8 d5 J if(rslt!=DBIERR_NONE)
" B+ o$ f- c( v! _ Application->MessageBox("不能压缩表", * G, M: ?+ `1 f( A3 y7 w4 W S
"压缩数据表出错",MB_ICONERROR);
: _6 P8 Y2 ?: V0 {$ l% P. Q}
3 M7 ?; U2 w3 ^注意,在Restructure之前,表必须处于关闭状态。以下例程调用PackParadoxTable.
! J' Q8 J$ @6 |" F5 U 9 \3 Z1 g* s, r' b* B
void __fastcall TForm1: ackTable(AnsiString table_name) # t; Y. D# U: n% a5 V
{
: m1 ?* q9 D8 ]) B3 P$ t$ |+ O //Pack the table ! Q2 h3 _& E* \! ]! o
TTable *temp_table=new TTable(Form1); 1 r* N( U* x& d: C# z
temp_table->DatabaseName="YourDatabaseAlias"; - g, J+ p; X( W9 I. g
temp_table->TableName=table_name; + l6 Q+ J+ O) W% Q; P% ~9 m# f
temp_table->Exclusive=true; : Z: S7 l- b( H) t# v |! R
temp_table->Open(); 5 g0 \8 q) l1 w2 {' [
//get the Database Handle $ q: e' d6 @* W5 F9 |$ \
hDBIDb hDB=temp_table->DBHandle; 6 o7 V1 b/ |* x! Z+ R) ?
temp_table->Close();
! S6 N2 D+ }; y0 j Z2 b
' {% _5 B6 v/ f, d- {, l, a7 d" U& n' x PackParadoxTable(hDB,table_name);
: D5 m; Q/ ]3 g; t
5 {8 K& t. m+ B* B temp_table->Close(); ( `0 X1 D5 M( g
temp_table->Free();
! s/ ]3 Y4 {9 k. R} & B8 ^) C1 D3 x
' H+ h( z; G0 l, K: M5 E - @6 ?# \' ]1 C
对Foxpro和DBase的Pack参见BDE API Help的DbiPackTable函数说明。 |