如何通过代码断开某SQL Server数据库应用程序连接?

fs_windy 2002-11-23 04:46:29
有一个数据库建立在SQL Server的应用程序,在我单位的局域网上,安装了该应用程序的计算机均在日常中使用,现本人想进行该SQL数据库备份时,由于客户端一直保持连接,而导致没法备份。

如何通过代码断开某SQL Server数据库应用程序所有连接?

我试过Kill,但返回信息为“需用户才能进行”

如果用Shutdown no wait,则备份命令没法继续,需要重新打开数据库才行。

高手求救啊~~~~~~~~~~~
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2002-11-23
  • 打赏
  • 举报
回复
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

--用法
use master
exec killspid '数据库名'
fs_windy 2002-11-23
  • 打赏
  • 举报
回复
To hjhing(winding):

sp_option 是什么存储过程,怎么提示无效没找到,不明白你的意思,请详细说明,谢谢。
hjhing 2002-11-23
  • 打赏
  • 举报
回复
設置成single user
sp_option 'yourdatabse','single user','true'
---do something
sp_option 'yourdatabse','single user','false'

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧