说明
1、该程序只是一个例子,并不完善,例如我没考虑各表之间的关系,如果你要实际使用
还要修改一下,加上错误处理,如果出错,先删除其他表,最后在删除出错的表
2、DAO2000单元在delphi的source目录下,你搜一下就找到了
3、压缩完成后,自增字段可以重新从1开始自增
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;
type
TForm1 = class(TForm)
Button1: TButton;
ADOConnection1: TADOConnection;
Button2: TButton;
ADOQuery1: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses DAO2000;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Engine
BEngine;
i:integer;
begin
for i:=0 to MDIChildCount-1 do begin
if MDiChildren
<>nil then
MDIChildren.Close;
end;
self.ADOConnection1.Close;
try
if FileExists(ExtractFilePath(Application.ExeName)+'Temp.mdb') then
DeleteFile(ExtractFilePath(Application.ExeName)+'Temp.mdb');
Engine:=CoDBEngine.Create;
Engine.CompactDatabase(ExtractFilePath(Application.ExeName)+'DB1.mdb',ExtractFilePath(Application.ExeName)+'temp.mdb','',0,'');
CopyFile(Pchar(ExtractFilePath(Application.ExeName)+'Temp.mdb'),Pchar(ExtractFilePath(Application.ExeName)+'DB1.mdb'),False);
finally
engine:=nil;
if FileExists(ExtractFilePath(Application.ExeName)+'Temp.mdb') then
DeleteFile(ExtractFilePath(Application.ExeName)+'Temp.mdb');
end;
ShowMessage('压缩完毕!');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
SL: TStrings;
index: Integer;
begin
self.ADOConnection1.Connected:=True;
SL := TStringList.Create;
ADOConnection1.GetTableNames(SL, False);
for index :=0 to (SL.Count - 1) do begin
if ADOQuery1.Active then ADOQuery1.Close;
ADOQuery1.SQL.Text:='Delete From '+SL[index]+' where 1=1';
ADOQuery1.ExecSQL;
end;
SL.Free;
button1.Click;
end;
end.