返回列表 发帖

[VC++] [转帖]MDB数据库压缩方法

mdb数据库压缩方法
《世界商业评论》icxo.com ( 日期:2004-12-03 15:19)

--------------------------------------------------------------------------------





mdb数据库压缩方法









虽然说mdb数据库功能不是很强大,但时由于在win x系统中已有缺省的驱动程序,所以不用另外像sql,sybase一样安装一个管理驱动,而且携带方便,很多的小应用程序或网站还是采用mdb数据库。经常操作mdb数据时,就会经常碰到要对数据进行压缩,下面的我在网上找到和自己使用的经验写一下关于在各种环境中压缩mdb数据库的方法,提供大家参考:






一、asp或vb中压缩



以前使用 dao 时,microsoft 有提供 compactdatabase method 来压缩 microsoft access 资料库,repairdatabase method 来修复损毁的 microsoft access 资料库。可是自从 ado 出来之后,也提供了解决方法,不过有版本上的限制!限制说明如下:



activex data objects (ado), version 2.1



microsoft ole db provider for jet, version 4.0



这是 microsoft 提出的 ado 的延伸功能:microsoft jet ole db provider and replication objects (jro)这个功能在 jet ole db provider version 4.0 (msjetoledb40.dll) 及 jro version 2.1 (msjro.dll) 中第一次被提出!这些必要的 dll 文件在您安装了 mdac 2.1 之后就有了,您可以在以下的网页中下载 mdac 的最新版本!在下载之前先到 vb6 中检查一下,【工程】【设定引用项目】中的 microsoft jet and replication objects x.x library 如果已经是 2.1 以上的版本,您就可以不用下载了!在您安装了 mdac 2.1 或以上的版本之后,您就可以使用 ado 来压缩或修复 microsoft access 资料库,下面的步骤告诉您如何使用 compactdatabase method 来压缩 microsoft access 资料库:1、开启一个新工程,点选功能表中的【工程】【设定引用项目】。



2、加入 microsoft jet and replication objects x.x library,其中 ( x.x 大于或等于 2.1 )。



3、在适当的地方加入以下的程序码,记得要修改 data source 的内容及目地资料库的路径:



dim jro as jro.jetengine
set jro = new jro.jetengine
jro.compactdatabase provider=microsoft.jet.oledb.4.0;data source=d:\nwind2.mdb, _ 来源资料库
provider=microsoft.jet.oledb.4.0;data source=d:\abbc2.mdb;jet oledb:engine type=4 目的资料库



在 dao 3.60 之后,repairdatabase method 已经无法使用了,以上的程序码显示了 ado compactdatabase method 的用法,而它也取代了 dao 3.5 时的 repairdatabase method



二、delphi中压缩



const sconnectionstring = provider=microsoft.jet.oledb.4.0;data source=%s; jet oledb:database password=%s;;



function gettemppathfilename():string;



var



spath,sfile:array [0..254] of char;
begin
gettemppath(254,spath);
gettempfilename(spath,~sm,0,sfile);
result:=sfile;
deletefile(result);
end;



function compactdatabase(afilename,apassword:string):boolean;
//压缩与修复数据库,覆盖源文件
var
stempfilename:string;
vjelevariant;
begin
stempfilename:=gettemppathfilename;
try
vje:=createoleobject(jro.jetengine);
vje.compactdatabase(format(sconnectionstring,[afilename,apassword]),
format(sconnectionstring,[stempfilename,apassword]));
result:=copyfile(pchar(stempfilename),pchar(afilename),false);
deletefile(stempfilename);
except
result:=false;
end;
end;



三、c builder中压缩



c 中在网上并没有找到很好的方法,但是根据我原来所作的建立mdb数据库方法,也找到了一个相对实用的的压缩方法;



1、 建立mdb文件的函数



#include



bool createaccessfile( string stfilename )



{



if( fileexists( stfilename )) return true ;






variant vcreateaccess;



procedure pcreateaccess( create );






pcreateaccess << provider=microsoft.jet.oledb.4.0;data source= stfilename ;



vcreateaccess = createoleobject( adox.catalog );



vcreateaccess.exec( pcreateaccess );






return ( fileexists( stfilename )) ;



}



//---------------------------------------------------------------------------



2、 由ole想到的功能来实现压结合实际的功能



#include



void compactdatabase( string stfilename )



{



variant vcreateaccess;



procedure pcreateaccess( compactdatabase );






string sttempfile = temp.mdb ;



if( fileexists(sttempfile)) deletefile(sttempfile) ;



pcreateaccess << provider=microsoft.jet.oledb.4.0;data source= stfilename ; ; //如果有密码请加上后面这一句jet oledb:database password=password; ;



pcreateaccess << provider=microsoft.jet.oledb.4.0;data source= sttempfile ; ; //如果有密码请加上后面这一句jet oledb:database password=password; ;



vcreateaccess = createoleobject( jro.jetengine );



vcreateaccess.exec( pcreateaccess );



//代替原来的数据库



copyfile(sttempfile.c_str(),stfilename.c_str(), (int)false ) ;



deletefile(sttempfile) ;



}



四、vc中压缩



#import c:program filescommon filessystemadomsjro.dll no_namespace add the following (specifying your own source and destination database paths) to the .cpp file where you
want to compact the database:




try
{
ijetengineptr jet(__uuidof(jetengine));
jet->compactdatabase(
provider=microsoft.jet.oledb.4.0;data source=d:\nwind2.mdb,
provider=microsoft.jet.oledb.4.0;data source=d:\abbc.mdb;
jet oledb:engine type=4);
}
catch(_com_error &e)
{
::messagebox(null, (lpctstr)e.description( ), , mb_ok) ;
}






更具体的e文可以看微软的文章:

TOP

返回列表

Powered by Discuz! 7.2   论坛QQ群:逐梦论坛群

© 2001-2021 Comsenz Inc. 鲁公网安备 37120302000001号