如题
谢了!

解决方案 »

  1.   

    以下是我程序里的代码
    //**********************************************
    //****************** 数据库备份 ****************
    //**********************************************
    procedure TFrmMain.MnuSys_BackUpDatabaseClick(Sender: TObject);
    var
      SourcePath: String;  //数据库备份路径
    begin
      SourcePath:=ExtractFileDir(Application.ExeName);
      if (strLen(PChar(SourcePath))<>3) then
         SourcePath:=SourcePath + '\';
      SourcePath:=SourcePath + 'DatabaseBackUp';
      Directory:=SourcePath;
      Dialog1.Title:='数据库备份';
      Dialog1.Execute;
      Dialog1.Title:='正在备份数据库';                   
      SourcePath:=Dialog1.Directory + '\数据库备份名称'.back';
      DM.DatabaseBackUp.CommandText:='backup database JSHR to disk=''' + SourcePath + '''';
      try                                                              
        DM.DatabaseBackUp.Execute;
        MessageDlg('-----  @ 数据库备份成功!@ -----',mtInformation,[mbOK],0);
      except
        MessageDlg('-----  @ 数据库备份失败!@ -----',mtInformation,[mbOK],0);
      end;
    end;//**********************************************
    //****************** 数据库还原 ****************
    //**********************************************
    procedure TFrmMain.MnuSys_ResumeDatabaseClick(Sender: TObject);
    var
      SourcePath: string;
    begin
      DM.ADOCNN.close;             //关闭数据库
      DM.ADOCNN.Connected:=False;
      Dialog1.Title:='还原数据库';
      SourcePath:=ExtractFileDir(Application.ExeName);
      if(strLen(Pchar(SourcePath))<>3) then
        SourcePath:=SourcePath + '\';
      SourcePath:=SourcePath + 'DatabaseBackUp';
      Dialog1.InitialDir:=SourcePath;
      Dialog1.Filter:='数据库备份文件(*.back)|*.back|所有文件(*.*)|*.*';   //设置备份文件后缀
      if Dialog1.Execute then
        if Dialog1.filename<>'' then
          begin
            if MessageDlg('-----  @ 此操作将使上次备份以来的所有数据丢失,是否继续?@ -----',
                                          mtConfirmation,[mbYes,mbNo],0)=mrYes then
              begin
                DM.DatabaseBackUp.CommandText:='restore database JSHR from disk=''' + bsSkinOpenDialog1.filename + '''' + 'with replace';
                try
                  DM.DatabaseBackUp.Execute;
                  MessageDlg('-----  @ 数据库恢复成功!@ -----',mtInformation,[mbOK],0);
                except
                  MessageDlg('-----  @ 数据库恢复失败!@ -----' + #13 + #13 + '数据库正在被使用,请确定己关闭其他使用该数据库的程序!',mtInformation,[mbOK],0);
                end;
                try     //尝试打开所有数据表
                  DM.ADOCNN.Open;
                  //.....................
                except
                  MessageDlg('-----  @ 程序运行发生不可预知错误,请重新启动程序!@ -----',mtInformation,[mbOK],0);
                  Application.Terminate;
                end;
              end;
          end;
    end;
      

  2.   

    procedure TFrm_XTWH.Button2Click(Sender: TObject);//还原原代码
    var
       PathName,DataName,DatabaseStr,StrTmp,ConnStr:string;
       i:integer;
    begin
    try
        PathName:=FileListBox1.FileName;
        DataName:=ExtractFileName(FileListBox1.FileName );
        if DataName='' then
        begin
            application.MessageBox('请选择还原名称!','提示',mb_ok);
            abort;
        end;
        ConnStr:=DataModule1.ADOConnection1.ConnectionString;
        if pos('Initial Catalog=',ConnStr)>0 then
        begin
            StrTmp:=rightStr(ConnStr,length(ConnStr)-pos('Initial Catalog=',ConnStr)-15);
            if pos(';',StrTmp)>0 then
            begin
                ConnStr :=stringreplace(ConnStr,leftStr(StrTmp,pos(';',StrTmp)-1),'master',[rfReplaceAll]);
            end
            else
            begin
                application.MessageBox('系统错误!','提示',MB_OK);
                abort;
            end;
        end
        else
        begin
            application.MessageBox('系统错误!','提示',MB_OK);
            abort;
        end;
        ADOConnection1.ConnectionString:=ConnStr;
        ADOConnection1.Open;
        adoquery1.Connection:=ADOConnection1;
        adoquery1.Close;
        adoquery1.SQL.Text:='select  spid  from  sysprocesses  where  dbid=db_id(''charge'')';
        adoquery1.Open;
        memo1.Lines.Add(ADOConnection1.ConnectionString) ;
        //memo1.Lines.Add(adoquery1.FieldValues['spid']) ;
        //memo1.Lines.Add(adoquery1.Fields('spid').ClassName ) ;
        while not adoquery1.Eof do
        begin
            ADOConnection1.Execute('kill '+adoquery1.fieldbyname('spid').AsString  ) ;
            adoquery1.Next;
        end;
        adoquery1.Close;
        DatabaseStr:='Restore Database Charge from disk='''+PathName+''' with Replace';
        memo1.Lines.Add(DatabaseStr);    ADOConnection1.Execute(DatabaseStr,cmdText,[eoExecuteNoRecords]);
        {ADOConnection1.BeginTrans;
        if ADOConnection1.Errors.Count = 0 then
        begin
            ADOConnection1.CommitTrans;
            application.MessageBox('还原成功!','提示',mb_ok);         
        end
        else
        begin
            ADOConnection1.RollbackTrans;
            for i:=0 to ADOConnection1.Errors.Count-1 do
            begin
                memo1.Lines.Add(adoconnection1.Errors.Item[i].Description ); 
            end;
            application.MessageBox('还原失败,请检查数据库是否已经打开!','提示',mb_ok);
        end;  }
             application.MessageBox('还原成功!','提示',mb_ok);
             ADOConnection1.Close;
            except
            application.MessageBox('系统错误!','提示',MB_OK);
            abort;
    end;
    end;
      

  3.   

    oupj() 的备份是可以的,在还原时,
    在单独运行是可以的.可在前台已与数据连接后,可能不行
    以前做过,与 oupj()的原还代码差不多.你可以试试,
    我也正在求解:
    用两面个ADOconnecion,一个用于连接,一个用备份和还原,
    还原后,恢复到还原前的状态.