Access 中如何使用 ADO 來壓縮或修復(fù) MS Access 文件?
2024-07-21 02:09:28
供稿:網(wǎng)友
本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
access 中如何使用 ado 來壓縮或修復(fù) ms access 文件?
專題地址:http://access911.net/index.asp?board=4&recordid=71fab71e
問題:
access 中如何使用 ado 來壓縮或修復(fù) microsoft access 文件?
回答:
以前使用 dao 時,microsoft 有提供 compactdatabase method 來壓縮 microsoft access 文件,repairdatabase method 來修復(fù)損壞的 microsoft access 文件,。可是自從 ado 出來之后,好像忘了提供相對的壓縮及修復(fù) microsoft access 文件的功能。
現(xiàn)在 microsoft 發(fā)現(xiàn)了這個問題了,也提供了解決方法,不過有版本上的限制!限制說明如下:
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 之后就有了,您可以在以下的網(wǎng)頁中下載 mdac 的最新版本!
universal data access web site
在下載之前先到 vbe 界面中檢查一下,【引用】中的 microsoft jet and replication objects x.x library 如果已經(jīng)是 2.1 以上的版本,您就可以不用下載了!
在您安裝了 mdac 2.1 或以上的版本之后,您就可以使用 ado 來壓縮或修復(fù) microsoft access 文件,下面的步驟告訴您如何使用 compactdatabase method 來壓縮 microsoft access 文件:
1、新建一個新表單,選擇 vbe 中【引用】。
2、加入 microsoft jet and replication objects x.x library,其中 ( x.x 大于或等于 2.1 )。
3、在適當(dāng)?shù)牡胤郊尤胍韵碌某绦虼a,記得要修改 data source 的內(nèi)容及目地文件的路徑:
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 已經(jīng)無法使用了,以上的程序代碼顯示了 ado compactdatabase method 的用法,而它也取代了 dao 3.5 時的 repairdatabase method!
附注:
1、出現(xiàn)錯誤提示(不能執(zhí)行這項操作;在低版本的數(shù)據(jù)庫中該版本的特性不可用。)是因為版本號與你的數(shù)據(jù)庫格式不符,請看下面的對照表
引擎版本號
jet oledb:engine type -> jet x.x format mdb files
1 -> jet10
2 -> jet11
3 -> jet2x
4 -> jet3x
5 -> jet4x
2、其他的壓縮修復(fù)數(shù)據(jù)庫的對象以及方法:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;240434
雖然有時可以成功地打開某些損壞的數(shù)據(jù)庫,但由于索引或表行已損壞,因此數(shù)據(jù)訪問速度要慢很多。可以運行 microsoft access 開發(fā)環(huán)境中的“修復(fù)和壓縮數(shù)據(jù)庫”實用程序來修復(fù)損壞的數(shù)據(jù)庫。編程時,您可以使用 jet and replication objects (jro) 的compactdatabase 方法、jet ole db 提供程序的ijetcompact::compact 方法,或 data access objects (dao) 的cdaodatabase::compactdatabase 方法,來實現(xiàn)此目的。
方法二:
delphi代碼(未驗證)
//uses comobj,activex
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;
//壓縮與修復(fù)數(shù)據(jù)庫,覆蓋源文件
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;
方法三:
vc請參考:
http://support.microsoft.com/default.aspx?scid=kb;en-us;230496
http://support.microsoft.com/?id=230501