--断开指定库的所有用户连接(在master数据库中进行)
use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_KillSpid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_KillSpid]
GOcreate proc sp_KillSpid
@dbname sysname --要断开连接的数据库名
as
declare @s nvarchar(1000)
declare tb cursor local
for
select N'kill '+cast(spid as varchar)
from master..sysprocesses
where dbid=db_id(@dbname)open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go--调用
exec sp_KillSpid 'aa'
--创建并调用上面的存储过程,断开用户的连接后再恢复就可以了.
use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_KillSpid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_KillSpid]
GOcreate proc sp_KillSpid
@dbname sysname --要断开连接的数据库名
as
declare @s nvarchar(1000)
declare tb cursor local
for
select N'kill '+cast(spid as varchar)
from master..sysprocesses
where dbid=db_id(@dbname)open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go--调用
exec sp_KillSpid 'aa'
--创建并调用上面的存储过程,断开用户的连接后再恢复就可以了.
我在vc中执行这个语句为什么会出错呢
CString pathName=FileDlg.GetPathName ();//这是我选中备份后的数据库文件路径
try
{
theApp.m_pConnection ->Execute ("exec sp_KillSpid 'film'",NULL,-1);
strQuery="restore database film from Disk ='"+pathName+"'";
if(theApp.m_pConnection ->Execute ((_bstr_t)strQuery,NULL,adCmdText))
{
AfxMessageBox("数据库恢复成功!");
}
}
catch(_com_error e)
{
...
return;
}
我应该这样执行语句
theApp.m_pConnection ->Execute ("use master exec sp_KillSpid 'film'",NULL,-1);
这是我的不够仔细造成的,不过我还是谢谢邹健先生!