MDB數據庫壓縮方法
2024-07-21 02:06:31
供稿:網友
 
  
mdb數據庫壓縮方法
[email protected]
 
雖然說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 的最新版本!universal data access web site在下載之前先到 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;
  vje:olevariant;
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 <comobj.hpp>
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 <comobj.hpp> 
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 files/common files ystem/ado/msjro.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文可以看微軟的文章:
http://support.microsoft.com/default.aspx?scid=kb;en-us;230496
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。