ADO压缩Access的错误,多谢了!(150分)

  • 主题发起人 主题发起人 隐儿
  • 开始时间 开始时间

隐儿

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ADO压缩数据库时,
出现了以下错误,

‘没有使用‘’对象的必要权限。
请让系统管理员或安装此对象
的人为您设置适当的权限’

但我的数据库没有密码呀,
代码如下,
多谢了!!
procedure TForm_Disk.CompactAndRepair(sOldMDB : String);
const
sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin';
var
oJetEng: JetEngine;
sNewMDB,Temp:String;
begin
Temp:=sOldMDB;
sOldMDB := sProvider + 'Data Source=' + sOldMDB;
sNewMDB := sProvider +'Data Source=' + 'tmpdb.bak;Jet OLEDB:Engine Type=5';
// try
Form1.ADOConnection1.Connected:=false;
Form1.ADOConnection1.Close;
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sOldMDB, sNewMDB);//这句出错,运行时错误
oJetEng := nil;
DeleteFile(sOldMDB);
RenameFile('tmpdb.bak',Temp);
Form1.ADOConnection1.Connected:=true;
Form1.ADOConnection1.Open;
 
我认为你先把所有的链接断掉:ADOConnection1.Connected:=false;
再用compactDatabase函数,带密码压缩的格式:
CompactDatabase (SourceFilename, TargetFileName, '', 0, ';pwd=你的密码');
 
CompactDatabase,参数
CompactDatabase(const SrcName: WideString; const DstName: WideString;
DstLocale: OleVariant; Options: OleVariant; SrcLocale: OleVariant); safecall;
写文件名就可以了
 
To forgot2002:
你的压缩方式是dao方式的,
而我的是ado方式的。

to door2000:
好像参数不对哦,
我的不是编译时错误,
是运行时错误
 
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:Database Password=%s;' //数据库密码
+ 'Jet OLEDB:Engine Type=1;'
+ 'Jet OLEDB:Database 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:Don''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:Database Password=' + '"' + DBpass + '"' + ';';
strDataDest := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + strDataDestName + ';Jet OLEDB:Database 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'
 
这个是使用Ado的。不用导入JetEnginner了。
procedure TForm1.Button2Click(Sender: TObject);
var
Dest_DB_Connection: OleVariant;
l_Source_Con_Str, l_Dest_Con_Str: string;
begin
try
Dest_DB_Connection := CreateOleObject('JRO.JetEngine');
except
Exit;
end;

l_Source_Con_Str := 'Data Source=C:/1.mdb;';

l_Dest_Con_Str := 'Data Source=C:/2.mdb;';

try
OleCheck(Dest_DB_Connection.CompactDatabase(l_Source_Con_Str, l_Dest_Con_Str));
except
end;
end;
 
假设此数据库的密码是 idlehagar

use JRO_TLB;
procedure CompactMdb;
const sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var oJetEng: JetEngine;
 sOldMdb, sNewMdb: string;
begin
 sOldMdb := sProvider + 'Data Source=' + 'C:/Database/Test.mdb;'
  + 'Jet OLEDB:Database Password=idlehagar';
 sNewMdb := sProvider + 'Data Source=' + 'C:/Test.mdb;'
  + 'Jet OLEDB:Database Password=idlehagar';
 try
  try
   oJetEng := CoJetEngine.Create;
   oJetEng.CompactDatabase(sOldMdb, sNewMdb);
   if FileExists('C:/Test.mdb') then begin
    CopyFile('C:/Test.mdb', 'C:/Database/Test.mdb', False);
    DeleteFile('C:/Test.mdb');
  end;
  except
   on E: Exception do
    showmessage('Compact database failed');
  end
 finally
  oJetEng := nil;
 end;
end;
 
我程序中用过,没问题。你试试
use JRO_TLB;

Function TMainform.CompactMDB(sOldMDB,sNewMDB,sPassword : String) : Boolean;
const sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var oJetEng : JetEngine;
begin
sOldMDB := sProvider + 'Data Source=' + CurrentDir + sOldMDB+';' +
'Jet OLEDB:Database Password='+sPassword;
sNewMDB := sProvider + 'Data Source=' + CurrentDir + sNewMDB+';' +
'Jet OLEDB:Database Password='+sPassword;
try
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sOldMDB, sNewMDB);
oJetEng := Nil;
Result := True;
except
oJetEng := Nil;
Result := False;
screen.Cursor:=crdefault;
end;
end;

调用
procedure TMainForm.button1Click(Sender: TObject);
begin
ADOConnection1.Connected :=false;//必须断开连接
if CompactMDB(原库名,新库名 ,密码) then begin
DeleteFile(原库名);
RenameFile(新库名 ,原库名);
MessageDlg('压缩数据成功!!',mtInformation,[mbOk],0);
end
else MessageDlg('压缩数据出错!请确认数据库不被使用……',mtError,[mbOk],0);
ADOConnection1.Connected :=true;
end;
 
試這個吧,你壓縮之前一定要把所有的連接都斷開:
var
dao: OLEVariant;
begin
screen.Cursor:=crHourGlass;
// dm.DataBase.Connected:=false;斷開連接
dao := CreateOleObject('DAO.DBEngine.36');
dao.CompactDatabase(d:/prog/data.mdb',d:/prog/data/tempdata.mdb','',0,'');
screen.Cursor:=crDefault;
Close;
end;
 
最简单的压缩方法:
var
CompactAccess: OleVariant;
begin
dm.ADOConnection1.Connected:=false;
CompactAccess:=CreateOleObject('JRO.JetEngine');
CompactAccess.CompactDatabase('Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
'Old.mdb', 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ 'New.mdb;'+
'Jet OLEDB:Encrypt Database=True');

DeleteFile('Old.mdb');
RenameFile('new.mdb','new.mdb');
dm.ADOConnection1.Connected:=true;
 
看我的代码,很简单的。。。。
uses JRO_TLB//一定要引用这个
const
sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var
oJetEng : JetEngine;
begin
ADOConnection1.Close;
//对access数据库进行整理维护...
try
oJetEng := CoJetEngine.Create; //建立ole对象
oJetEng.CompactDatabase(sProvider+'Data Source=db1.mdb',sProvider+'Data Source=db9.mdb');//整理数据库到新的文件
DeleteFile('db1.mdb');//删除原来的文件
RenameFile('db9.mdb','db1.mdb');//重新命名
oJetEng := Nil;
except
oJetEng := Nil;
end;
AdOConnection1.Open;
//最后,你要知道,access本身所谓‘压缩整理’,也是这么干的,拷贝->删除->重新命名。。。
 
用个压缩控件吧。
很方便,而且不易出问题。
 
后退
顶部