--存储过程
use master
go
if object_id('killspid','P') is not null
drop proc killspid
go
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
调用进程:
/// <summary>
/// 杀死当前库的所有进程
/// </summary>
/// <returns></returns>
private bool exepro()
{
SqlConnection conn1 = new SqlConnection("server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=master");
SqlCommand cmd = new SqlCommand("killspid", conn1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dbname", database);
try
{
conn1.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
conn1.Close();
}
}