zoukankan      html  css  js  c++  java
  • sql 备份还原

    先记下来,空了去看看

         //开始备份

          with adoquery1 do

           begin

                 close;

                 sql.Clear;

                 sql.Add('Backup database library to disk=:p1 with init');

                 parameters.ParamByName('p1').Value:=self.SaveDialog1.FileName;

                try

                    Execsql;

                   ShowMessage('备份成功!!');

                except

                    ShowMessage('备份失败!!');

                    exit;

                end;

           end; //End of 备份

      下面来说一下还原,还原与备份不一样,备份不需要关闭数据库,但还原就要先关闭数据库才行.第一次写的

    代码如下:

           with ADOQuery1 do

             begin

                Close;

                SQL.Clear;

                SQL.Add('use master alter database library set offline WITH ROLLBACK IMMEDIATE ');

                SQL.Add('restore database library from disk=:p1 with REPLACE');

                SQL.Add('alter database library set online with rollback immediate');

                parameters.ParamByName('p1').Value := self.Edit1.Text;

                try

                  ExecSQL;

                  ShowMessage('还原成功!!');

                except

                  Showmessage('还原失败!!');

                  exit;

                end;

             end;

       运行,还原成功!!但是數據庫會變爲脫機狀態,導致其他用戶不能連接。解決的辦法有两种:

     一:用存储过程:

      在master数据库中加入下面的存储过程

    if exists(select name from sysobjects where name='usp_restoredb')

     drop proc usp_restoredb

    go

    /*

    exec usp_restoredb "library","D:ak.bak"

    */

    create proc usp_restoredb

    (

     @dbname  varchar(255),   --数据库名字

     @filepath varchar(255)    --文件路径

    )

    as

    set nocount on

    --exec ('use master go')

    exec ('alter database '+@dbname+' set offline WITH ROLLBACK IMMEDIATE')

    exec ('restore database '+@dbname+' from disk="'+@filepath+'" with REPLACE')

    exec ('alter database '+@dbname+' set online with rollback IMMEDIATE')

    if @@error<>0

    begin

     select 'F','数据库恢复失败'

     return

    end

    else

    begin

     select 'T','数据库恢复成功'

     return

    end

    运行.

      然后在Delphi的调用这一存储过程,调用过程如下:

    //注意:这时的ADOQuery1要连到master数据库,不能连到library数据库!! 

    with ADOQuery1 do

        Begin

          Close;

          SQL.Clear;

     // filename是你备份文件的路径加文件名

          SQL.Add('exec usp_restoredb "Library","'+ filename+'"');

          Open;

          if ADOQuery1.Fields[0].AsString='F' then

            ShowMessage('还原失败!!')

          else

            ShowMessage('还原成功!!');

        end;

     运行成功!且library数据库不会变为脱机!

     第二种方法:

      也是运行ADOQuery1来实现,不用存储过程,ADOQuery1一定要连到master数据库,否则就会让library脱机!,代码如下:

    begin

      DataModule17.ADOConnection1.Close;//这个是连到library数据库的,所以要先断开

     with ADOQuery1 do

             begin

                Close;

                SQL.Clear;

                SQL.Add('use master alter database library set offline WITH ROLLBACK IMMEDIATE ');

                SQL.Add('restore database library from disk=:p1 with REPLACE');

                SQL.Add('alter database library set online with rollback immediate');

                parameters.ParamByName('p1').Value := self.Edit1.Text; //显示备份文件的路径和文件

                try

                  ExecSQL;

                  ShowMessage('还原成功!!');

                except

                  Showmessage('还原失败!!');

                  exit;

                end;

             end;

      end; //End Of 还原数据库

    运行成功!library数据库被还原

  • 相关阅读:
    MySQL server has gone away 问题的解决方法
    MySQL批量SQL插入性能优化
    mysql中int、bigint、smallint 和 tinyint的区别详细介绍
    Mac OS使用ll、la、l等ls的别名命令
    Github上的PHP资源汇总大全
    svn代码版本管理总结
    mysql information_schema介绍
    redis 五种数据结构详解(string,list,set,zset,hash)
    git 换行符LF与CRLF转换问题
    php 利用activeMq+stomp实现消息队列
  • 原文地址:https://www.cnblogs.com/760044827qq/p/3833909.html
Copyright © 2011-2022 走看看