加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver 数据库还原

发布时间:2020-12-12 14:38:15 所属栏目:MsSql教程 来源:网络整理
导读:首先,要建立存储过程killspid: create proc killspid (@dbname varchar(20)) as begindeclare @sql nvarchar(500)declare @spid intset @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id(' exec (@sql) open getspid fet

首先,要建立存储过程killspid:

create proc killspid (@dbname varchar(20))

as

begindeclare @sql nvarchar(500)declare @spid intset @sql='declare getspid cursor for

select spid from sysprocesses where dbid=db_id('

exec (@sql)

open getspid

fetch next from getspid into @spid

while @@fetch_status<>-1

begin

exec('kill )

fetch next from getspid into @spid

end

close getspid

deallocate getspidend

GO

然后,还原数据库需要执行的操作:

SqlConnection conn = new SqlConnection("Server=.;Database=master;User id=sa;Password=sa"); //注意要连接master数据库

conn.Open();

SqlCommand cmd1 = new SqlCommand("killspid",conn);

cmd1.CommandType = CommandType.StoredProcedure;

cmd1.Parameters.Add(new SqlParameter("@dbname","你的数据库名"));

cmd1.ExecuteNonQuery(); //先杀死数据库的进程

string sql = "Restore Database ljl_loftex From DISK='备份的路径'";

SqlCommand cmd2 = new SqlCommand(sql,conn);

cmd2.ExecuteNonQuery();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读