unit zip;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ComObj;
type
TForm1 = class(TForm)
dlgOpen1: TOpenDialog;
lbl1: TLabel;
edt1: TEdit;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
if dlgOpen1.Execute then
edt1.Text := dlgOpen1.FileName ;
end;
procedure TForm1.btn2Click(Sender: TObject);
var
DaoVar: OLEVariant;
begin
if Trim(edt1.Text)='' then //edit1用來儲存資料庫的路徑
begin
Application.MessageBox('你還未選擇需要壓縮的資料庫!','提示');
Exit;
end;
try
DaoVar := CreateOleObject('DAO.DBEngine.36'); //建立對象
if FileExists('temp.mdb') then DeleteFile('temp.mdb'); //刪除臨時資料庫檔案"temp.mdb"
DaoVar.CompactDatabase(edt1.Text, 'temp.mdb','',0,';pwd=1j2/n/#yi67#@&*/u/t'); //壓縮選中的資料庫到"temp.mdb" CompactDatabase函數是整個的核心
//pwd後面跟你自己資料庫的密碼,如果沒有密碼這個可以不寫
if DeleteFile(edt1.Text) then //刪除檔案原資料庫
RenameFile('temp.mdb', 'yizero.mdb'); //將"temp.mdb"改名為未經處理資料庫的名稱
Application.MessageBox('資料庫壓縮成功!','恭喜');
except
Application.MessageBox('抱歉,壓縮失敗,請檢查資料庫是否已開啟,請稍後重試!','出錯了');
end;
end;
end.
//注意在uses裡加上ComObj 而且在Import Type Library裡加上Microsoft Jet and Replication Objects Library
//另外,注意路徑和檔案名稱不要出錯
昨天突然發現Access資料庫居然到了50M了,確實很吃驚,因為資料庫裡並沒有多少東西,然後進Access用內建的工具壓縮、修複了一下,大小就正常多了!資料庫檔案太大了弊端肯定不少,但站在使用者的角度來看,他肯定不想自己進資料庫去做這個操作,而且我們也擔心他自己不小心改了表結構引起程式的異常甚至錯誤,所以就想到做這個小工具了。本文只針對遇到相同問題的初學者,高手請跳過!呵呵~~
程式介面如所示:
see Yizero by yizero.com