
如何恢复从C#数据库
1个回答
展开全部
首先,要建立存储过程killspid:
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @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 getspid
end
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();
不明白的可以追问。
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @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 getspid
end
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();
不明白的可以追问。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询