我以前用的方法是:备份用代码copy,恢复的话,让用户手工copy文件覆盖,感觉不规范。请问有更好的办法吗?谢谢~

解决方案 »

  1.   

    如果整个数据库备份的话就用copy,恢复的时候删掉原来的再copy过来不就行了
    如果是单个表的话又另外了。
      

  2.   

    思路:
    1、首先关闭所有和你的access链接的程序
    2、压缩数据库后复制到其他目录,代码如下;
    unit CompactF;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, RzCommon, StdCtrls, Mask, RzEdit, RzButton, publicfun2, comobj,
      shellapi, TLHelp32, RzLabel;type
      Tcomf = class(TForm)
        RzButton4: TRzButton;
        dbfile: TRzEdit;
        RzButton1: TRzButton;
        RzButton2: TRzButton;
        RzButton3: TRzButton;
        OpenDialog3: TOpenDialog;
        RzFrameController1: TRzFrameController;
        terapp: TRzEdit;
        RzButton5: TRzButton;
        state: TRzLabel;
        SaveDialog1: TSaveDialog;
        RzButton6: TRzButton;
        procedure RzButton2Click(Sender: TObject);
        procedure RzButton1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure RzButton4Click(Sender: TObject);
        procedure RzButton3Click(Sender: TObject);
        procedure RzButton5Click(Sender: TObject);
        procedure RzButton6Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        function GetExePathbyPName(fn: string): string;
        { Private declarations }
      public
        { Public declarations }
      end;var
      comf: Tcomf;
      exefN: string;
      exeP: string;
    implementation
    uses dbm;
    {$R *.dfm}
    ////////////////////////压缩数据库procedure CompactAccess(dbName: string; JetId: string = '4.0');
    var
      AVariant: Variant;
    begin
      if FileExists(dbName + '.tmp') then DeleteFile(DbName + '.tmp'); //是否存在相同的.tmp的临时文件,有则删了
      AVariant := CreateOleObject('JRO.JetEngine');
      AVariant.CompactDataBase('Provider=Microsoft.Jet.OLEDB.' + JetId + ';Jet OLEDB:Database Password=hjp;Data Source=' +
        dbName,
        'Provider=Microsoft.Jet.OLEDB.' + JetId + ';Data Source=' + dbName + '.tmp');
      DeleteFile(DbName);
      ReNameFile(dbName + '.tmp', DbName); //把文件名改回来
    end;function CompactDatabase(AFileName, APassWord: string): boolean; //压缩与修复数据库,覆盖源文件
    const
      SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
        + 'Jet OLEDB:Database Password=%s;';
    var
      SPath, SFile: array[0..254] of Char;
      STempFileName: string;
      JE: OleVariant;
    begin
      GetTempPath(40, SPath); //取得Windows的Temp路径
      GetTempFileName(SPath, '~CP', 0, SFile); //取得Temp文件名,Windows将自动建立0字节文件
      STempFileName := SFile; //PChar->String
      DeleteFile(STempFileName); //删除Windows建立的0字节文件
      try
        JE := CreateOleObject('JRO.JetEngine'); //建立OLE对象,函数结束OLE对象超过作用域自动释放
        OleCheck(JE.CompactDatabase(format(SConnectionString, [AFileName, APassWord]),
          format(SConnectionString, [STempFileName, APassWord]))); //压缩数据库
        //复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
        result := CopyFile(PChar(STempFileName), PChar(AFileName), false);
        DeleteFile(STempFileName); //删除临时文件
      except
        result := false; //压缩失败
      end;
    end;function GetNowProcesseIDbyName(f: string): integer;
    var
      ContinueLoop: BOOL;
      FSnapshotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
      ExeFile: string;
    begin
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      while integer(ContinueLoop) <> 0 do
      begin
        ExeFile := FProcessEntry32.szExeFile;
        if Uppercase(trim(ExeFile)) = Uppercase(trim(f)) then
        begin
          result := FProcessEntry32.th32ProcessID;
          break;
        end;
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
    end;function tcomf.GetExePathbyPName(fn: string): string;
    var
      modSnapShot: THandle;
      mProcess: TMODULEENTRY32;
      ret: Boolean;
    begin
      modSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetNowProcesseIDbyName(fN)); //模块快照
      mProcess.dwSize := sizeof(TMODULEENTRY32); //初始化TMODULEENTRY32结构大小
      ret := module32first(modSnapShot, mProcess); //第一个模块
      result := (mProcess.szExePath);
    end;function KillTask(ExeFileName: string): Integer;
    const
      PROCESS_TERMINATE = $0001;
    var
      ContinueLoop: BOOL;
      FSnapshotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
    begin
      Result := 0;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      while Integer(ContinueLoop) <> 0 do
      begin
        //showmessage(ExtractFileName(FProcessEntry32.szExeFile)+#1310+FProcessEntry32.szExeFile);
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
          UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
          UpperCase(ExeFileName))) then
          Result := Integer(TerminateProcess(
            OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0));
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
      CloseHandle(FSnapshotHandle);
    end;
    procedure Tcomf.RzButton2Click(Sender: TObject);
    begin
      if fileexists(dbfile.Text) then
        opendialog3.FileName := dbfile.Text;
      if opendialog3.Execute then
      begin
        dbfile.Text := opendialog3.FileName;
        state.Caption := '请点击按钮进行相应操作        ';
      end;
    end;procedure Tcomf.RzButton4Click(Sender: TObject);
    begin
      exefN := terapp.Text;
      if checkAppExists(exefN) then
      begin
        exep := GetExePathbyPName(exefN);
        KillTask(exefN);
        state.Caption := '已经中止了进程 smt.exe          ';
      end
      else
        state.Caption := '请选择要压缩的数据库文件            ';end;procedure Tcomf.FormCreate(Sender: TObject);
    begin
      if paramcount > 0 then
        exefN := paramstr(1);  if checkAppExists(exefN) then
      begin
        exep := GetExePathbyPName(exefN);
        KillTask(exefN);
        state.Caption := '已经中止了进程 smt.exe';
      end
      else
        state.Caption := '请选择要压缩的数据库文件            ';
      terapp.Text := ParamStr(1);
      dbfile.Text := ParamStr(2);
    end;
    ////procedure Tcomf.RzButton1Click(Sender: TObject);
    begin
      if fileExists(dbfile.Text) then
      begin
        state.Caption := '正在压缩数据库...        ';
        if CompactDatabase(dbfile.Text, '') then
          state.Caption := ('数据库压缩成功         ');
      end;
    end;procedure Tcomf.RzButton3Click(Sender: TObject);
    var fN: string;
    begin
      if fileExists(dbfile.Text) then
      begin
        if savedialog1.Execute then
        begin
          fn := savedialog1.FileName;
          if FileExists(fn) then
          begin
            showmessage('文件已经存在,请另选择一个文件! ');
          end
          else
          begin
            if fileExists(dbfile.Text) then
            begin
              state.Caption := '正在压缩数据库...        ';
              if CompactDatabase(dbfile.Text, '') then
              begin
                state.Caption := '正在备份数据库...        ';
                CopyFile(Pchar(dbfile.Text), pchar(fn), true);
                state.Caption := '数据库备份完毕...        ';
              end;
            end;
          end;
        end;
      end;
    end;procedure Tcomf.RzButton5Click(Sender: TObject);
    var
      fn: string;
    begin
      if fileExists(dbfile.Text) then
      begin
        state.Caption := '数据库还原完毕...        ';
        if opendialog3.Execute then
        begin
          fn := opendialog3.FileName;
          CopyFile(pchar(fn), Pchar(dbfile.Text), true);
          state.Caption := '数据库恢复完毕...        ';
        end;
      end;
     // MessageBox(self.Handle ,'正在查找备份的数据库!','提示',MB_OK+MB_ICONINFORMATION)
    end;procedure Tcomf.RzButton6Click(Sender: TObject);
    begin
      close;
    end;procedure Tcomf.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      if length(exep) > 0 then
        URLink(pchar(exep));
    end;end.