日期:2009-09-26  浏览次数:20447 次

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('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
exec('kill '+@spid)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
GO


在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)
 

using System;

using System.Configuration;

using System.Data.SqlClient;

using System.Data;

namespace web.base_class

{

     /// <summary>

     /// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复

     /// </summary>

     public class DbOper

     {

          private string server;

          private string uid;

          private string pwd;

          private string database;

          private string conn;

         /// <summary>

         /// DbOper类的构造函数

         /// </summary>

         public DbOper()

         {

              conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();

              server=cut(conn,"server=",";");

              uid=cut(conn,"uid=",";");

              pwd=cut(conn,"pwd=",";");

              database=cut(conn,"database=",";");

         }

         public string cut(string str,string bg,string ed)

         {

              string sub;

              sub=str.Substring(str.IndexOf(bg)+bg.Length);

              sub=sub.Substring(0,sub.IndexOf(";"));

              return sub;

         }

 

         /// <summary>

         /// 数据库备份

         /// </summary>

         public  bool DbBackup(string url)

         {

              SQLDMO.Backup oBackup = new SQLDMO.BackupClass();

              SQLDMO.SQLServer oSQLServer =