1

我正在使用 Delphi 程序来压缩访问数据库

程序的代码片段是:

procedure CompactDatabase(pFullDatabasePathName : string; pLoginName : string = '';    pPassword : string = ''; pSystemDb : string = '');

var
  JE            : TJetEngine;

  sdbTemp       : String;

  sdbTempConn   : String;

  sdbSrcConn    : String;

  loginString   : String;

  systemDbString: String;

  compactDone   : Boolean;

  const
    SProviderAccess2007 = 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=';

    SEngine = ';Jet OLEDB:Engine Type=';
    Access2007EngineType = '5';

 begin

  loginString := '';

  if (pLoginName <> '') then
    loginString := ';User Id= ' + pLoginName + '; Password = ' + pPassword;

  if ((pLoginName = '') and (pPassword <> '')) then
    loginString := ';Jet OLEDB:Database Password = ' + pPassword;


  systemDbString := '';

  if (pSystemDb <> '') then
    systemDbString := ';Jet OLEDB:System Database = ' + pSystemDb;


    try
      compactDone := False;
      JE := TJetEngine.Create(Application);

      sdbTemp := ExtractFileDir(pFullDatabasePathName) + '\TEMP' + ExtractFileName(pFullDatabasePathName);

      if FileExists(sdbTemp) then
        DeleteFile(sdbTemp);


      //Access 2007
      if not compactDone then
        begin
          try
            sdbSrcConn := SProviderAccess2007 + pFullDatabasePathName + loginString + systemDbString + SEngine + Access2007EngineType;
            sdbTempConn := SProviderAccess2007 + sdbTemp + SEngine + Access2007EngineType;
          JE.CompactDatabase(sdbSrcConn, sdbTempConn);
            compactDone := True;
          except
          end;
        end;

      if not compactDone then
        Raise Exception.Create('Compacting of database: ' + pFullDatabasePathName + 'failed!');

      if (not DeleteFile(pFullDatabasePathName)) then
        Raise Exception.Create('Compacting failed because cannot delete database: ' +  pFullDatabasePathName);

      if (not RenameFile(sdbTemp, pFullDatabasePathName)) then
        Raise Exception.Create('Compacting failed because cannot overwrite database: ' +  pFullDatabasePathName + ' by temporary compacted database: ' +   sdbTemp);

        if FileExists(sdbTemp) then
          DeleteFile(sdbTemp);

  finally
    JE.FreeOnRelease;
  end;
 end;

此过程的结果是 Compact Access 数据库,但采用 Access 2002-2003 格式。

我无法找到问题区域。是否需要更新某些 dll 或注册表设置?请帮忙...

4

1 回答 1

0

感谢 4 您的支持,我找到了解决此问题的有效方法,并发现它值得与您分享。

现在我不使用 Jet 4,而是使用“DAO.DBEngine.120”。

  //Access 2007 
  if not compactDone then 
    begin 
      try
       // DaoAccess2007 is OleVariant Type

        DaoAccess2007 := CreateOleObject('DAO.DBEngine.120');
        DaoAccess2007.CompactDatabase(pFullDatabasePathName,sdbTemp); 
        compactDone := True; 
      except 
      end; 
    end;

结果是 Access 2007 格式的压缩数据库。干杯!

于 2010-10-06T09:32:29.993 回答