求还原数据库文件代码

解决方案 »

  1.   

    什么数据库?
    sql sever 
    http://topic.csdn.net/t/20050309/21/3838323.html
    access
    http://waphi.baidu.com/othershore/blog/item/1adc0d1e97abc711413417ba.html?from=pc
      

  2.   

       ADOC:=TADOConnection.Create(Self);
       ADOC.LoginPrompt:=False;
       ADOC.ConnectionString:=Format(sConnectionStr,['master',sServerName]);
       try
       try
         ADOC.Open;
         ExeComm:=TADOCommand.Create(Self);
         ExeComm.Connection:=ADOC;
         ExeComm.CommandText:='use master';
         try
            ExeComm.Execute;
            ExeComm.CommandText:='ALTER DATABASE ' + sDBName +
              ' SET OFFLINE WITH ROLLBACK IMMEDIATE';
            ExeComm.Execute;
            ExeComm.CommandText:='Restore DataBase ' + sDBName +
              ' from Disk = ''' + sFileName + ''' With Replace';
            ExeComm.Execute;
            ExeComm.CommandText:='ALTER DATABASE ' + sDBName +
              ' SET ONLINE WITH ROLLBACK IMMEDIATE';
            ExeComm.Execute;
            MessageBox(Handle,'数据库恢复成功!','提示:',
              Mb_OK + Mb_IconInformation);
         except
           on e: Exception do begin
             MessageBox(Handle,PChar('恢复数据库失败:' + #13 +
               e.Message),'提示:',Mb_OK + Mb_IconError);
             ExeComm.Free;
         end;
       end;
       except
         on e: Exception do
           MessageBox(Handle,PChar('数据库连接失败: ' + #13 + e.Message),
             '提示:',Mb_OK + Mb_IconError);
       end;
       finally
          ADOC.Free;
       end;
      

  3.   

    procedure Tsys_datamodule.DataBaseBack;
    var
      FileName:string;
      ADOQ:TADOQuery;
    begin
      ADOQ:=TADOQuery.Create(nil);
      ADOQ.Connection:=sys_datamodule.con1;
      if MessageDlg('你確定要備份數據庫嗎?',mtInformation,[mbYes,mbNo],0)=idyes then
      begin
        try
          if SaveDialog1.Execute then
          begin
            FileName:=SaveDialog1.FileName;
            if (Copy(FileName,Length(FileName)-3,4)<>'.BAK') or (Copy(FileName,Length(FileName)-3,4)<>'.bak') then
              FileName:=FileName+'.BAK';
            ADOQ.SQL.Text:='Use Master Backup Database MES to Disk='''+ FileName+''' Use MES';
            ADOQ.ExecSQL;
            MessageDlg('數據庫備份成功!',mtInformation,[mbOK],0);
          end;
        except
          MessageDlg('數據庫備份失敗!',mtInformation,[mbOK],0);
          Exit;
        end;
      end;
    end;procedure Tsys_datamodule.RestoreBaseBack;
    var
      StrFile:string;
      ADOQ:TADOQuery;
    begin
      ADOQ:=TADOQuery.Create(nil);
      ADOQ.Connection:=sys_datamodule.con1;
      if MessageDlg('你確定要還原數據庫嗎?',mtInformation,[mbYes,mbNo],0)=idyes then
      begin
        try
          if OpenDialog1.Execute then
          begin
            StrFile:=OpenDialog1.FileName;
            ADOQ.SQL.Text:='Use Master Restore Database MES From Disk='''+ StrFile+''' Use MES';
            ADOQ.ExecSQL;
            MessageDlg('數據庫還原成功!',mtInformation,[mbOK],0);
          end;
        except
          MessageDlg('數據庫還原失敗!',mtInformation,[mbOK],0);
          Exit;
        end;
      end;
    end;