unit fununit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, shellapi, ADODB, ActiveX, JRO_TLB;
const
{TADOConnection 连接数据库的参数}
ADOLinkString = 'Provider=Microsoft.Jet.OLEDB.4.0;'
+ 'Password=%s;' //用户工作组(*.mdw)密码
+ 'User ID=%s;' //用户工作组(*.mdw)用户名Admin
+ 'Data Source=%s;' //数据库文件(*.mdb)位置
+ 'Persist Security Info=True;'
+ 'Mode=ReadWrite;' // 数据库打开方式
+ 'Extended Properties="";' //默认值为空
+ 'Jet OLEDB:System Database="";' //用户工作组文件
+ 'Jet OLEDB:Registry Path="";' //注册路径
+ 'Jet OLEDB
atabase Password=%s;' //数据库密码
+ 'Jet OLEDB:Engine Type=1;'
+ 'Jet OLEDB
atabase Locking Mode=1;'
+ 'Jet OLEDB:Global Partial Bulk Ops=2;'
+ 'Jet OLEDB:Global Bulk Transactions=1;'
+ 'Jet OLEDB:New Database Password=%s;' //?密码
+ 'Jet OLEDB:Create System Database=False;'
+ 'Jet OLEDB:Encrypt Database=False;'
+ 'Jet OLEDB
on''t Copy Locale on Compact=False;'
+ 'Jet OLEDB:Compact Without Replica Repair=False;'
+ 'Jet OLEDB:SFP=False';
procedure CompactDB(const DBname, DBtype, DBpass: string);
implementation
procedure CompactDB(const DBname, DBtype, DBpass: string);
var
MyJetEngine: JetEngine;
strDataSource,
strDataDest,
strDataDestName: string;
begin
if not FileExists(DBname) then
begin
writeln('Error: ''', DBName, ''' not found.');
exit;
end;
// delete any previous temporary mdb file
strDataDestName := ExtractFilePath(DBname) + 'temp.mdb';
if FileExists(StrDataDestName) then
begin
DeleteFile(strDataDestName);
//writeln('Previous temporary file ', strDataDestName, ' deleted.');
end;
strDataSource := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBname + ';Jet OLEDB
atabase Password=' + '"' + DBpass + '"' + ';';
strDataDest := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + strDataDestName + ';Jet OLEDB
atabase Password=' + '"' + DBpass + '"' + ';';
// default to Access 2000 format unless 97 format is specified
{ default changed 30 Sep 2000 DRS }
if DBtype = '97' then
// Use Engine Type 4 for Access 97 db
strDataDest := strDataDest + 'Jet OLEDB:Engine Type=4'
else
// Use Engine Type 5 for Access 2000 db
strDataDest := strDataDest + 'Jet OLEDB:Engine Type=5';
MyJetEngine := CoJetEngine.Create();
try
MyJetEngine.CompactDatabase(strDataSource, strDataDest);
//writeln(DBname, ' compacted into ', strDataDestName + '.');
MyJetEngine := nil;
if DeleteFile(DBname) then
begin
// writeln(DBname, ' deleted.');
if RenameFile(strDataDestName, DBName) then
// writeln(strDataDestName, ' renamed ', DBname + '.')
else
// writeln('Error: ', strDataDestName, ' could not be renamed ', DBname, '.');
end
else
// writeln('Error: ', DBname, ' could not be deleted.');
except
//on E: Exception do writeln('Error: ', E.Message);
end
end;
end.
主程序
调用
begin
opini := Tinifile.create(GetCurrentDir + '/tywdoption.ini');
try
mdwPassword := opini.readstring('database', 'mdwPassword', '');
mdwUserID := opini.readstring('database', 'mdwUserID', '');
mdbUserID := opini.readstring('database', 'mdbUserID', '');
mdbPassword := opini.readstring('database', 'mdbPassword', '');
mdbNewPassword := opini.readstring('database', 'mdbNewPassword', '');
mdbDataSource := getCurPath + opini.readstring('database', 'mdbDataSource', '');
finally
opini.Free;
end;
strConn := Format(ADOLinkString, [mdwPassword, mdwUserId, mdbDataSource, mdbPassword, mdbNewPassword]); {初始连接参数}
with ADOConnection1 do
begin
if Connected then Connected := false;
if strConn <> '' then ConnectionString := strConn;
Open;
end;
end;
压缩数据库调用
begin
dm1.ADOConnection1.Connected := false;
MessageDlg('请点击 OK 开始压缩,请耐心等待! ', mtInformation,
[mbOk], 0);
CompactDB(GetCurrentDir + '/CNFJWD.dat', '2000', '1+2+3=6?yes');
MessageDlg(' 压缩完毕! ', mtInformation,
[mbOk], 0);
dm1.ADOConnection1.Connected := true;
end;
tywdoption.ini 文件
[skinoption]
skin=3
[database]
mdwPassword=
mdwUserID=Admin
mdbUserID=Admin
mdbPassword="1+2+3=6?yes"
mdbNewPassword=
mdbDataSource='CNFJWD.dat'