X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sql :.net还原数据库

Posted on 2008-06-18 11:57  X龙  阅读(212)  评论(0编辑  收藏  举报

//杀掉数据库相关进程

create proc killspid

@dbName nvarchar(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

//在asp.net中调用存储过程 
        private void killProc(string dbName)
        {
            SqlConnection con;
            SqlCommand cmd;

            con = new SqlConnection();
            cmd = new SqlCommand();
            con.ConnectionString = "your connectionString"
            cmd.Connection = con;
            cmd.CommandText = "killspid";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@dbName", dbName);

            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

点击这里给我发消息http://wp.qq.com/index.html