// Kill database-related processes
Create proc killspid
@ Dbname nvarchar (20)
As
Begin
Declare @ SQL nvarchar (500)
Declare @ spid int
Set @ SQL = 'Clare getspid cursor
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
// Call the stored procedure in 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 ();
}
}