sqlserver 数据库还原
首先,要建立存储过程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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |