Delphi下壓縮Access資料庫

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.