如何在 SQL Server Management Studio 中关闭与本地数据库的所有连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23739419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you close all connections to a local database in SQL Server Management Studio?
提问by GIVE-ME-CHICKEN
I am trying to detach a local database, but getting errors because there are existing connections to it.
我正在尝试分离本地数据库,但由于存在与它的连接而出错。
How do I close existing connections on my local database?
如何关闭本地数据库上的现有连接?
PS. Running on SQL Server 2008 Express
附注。在 SQL Server 2008 Express 上运行
采纳答案by TomTom
Take it offline first. THe dialog for that allows a force option. Then you can detach it safely.
先脱机。该对话框允许强制选项。然后你可以安全地分离它。
回答by M.Ali
Disconnect All the Other Users to Your Database
断开所有其他用户与您的数据库的连接
ALTER DATABASE [YourDbName]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Disconnect your SSMS Connections to Database
断开 SSMS 与数据库的连接
If you are the only User connected to a database and want to disconnect all the connection made by you in SSMS you can goto any of the Query windows
open on SSMS and Right Click and see below:
如果您是连接到数据库的唯一用户,并且想要断开您在 SSMS 中建立的所有连接,您可以转到 SSMS 上的任何Query windows
打开并右键单击,如下所示:
回答by Philip Kelley
Manually (and thus, with a bit of effort, programmatically) you can use the KILL
command to summarily close open connections to the database.
手动(因此,通过一些努力,以编程方式)您可以使用该KILL
命令来临时关闭与数据库的打开连接。
Identifying which conenctions to close, now that's the hard part. Start with system procedures SP_WHO
and SP_WHO2
to manually identify what connections are using which databases; use and analysis of these procedures may lead to referencing system objects sys.processes, sys.dm_exec_sessions, and a host of others, followed by an eventual understanding of SPIDs and an inevitable respectful loathing of connection pools.
确定要关闭的连接,现在是困难的部分。开始有系统程序SP_WHO
和SP_WHO2
手动识别连接使用的是什么哪些数据库; 对这些过程的使用和分析可能会导致引用系统对象 sys.processes、sys.dm_exec_sessions 和许多其他对象,然后最终了解 SPID 并不可避免地尊重连接池。
All of these are covered in detail in SQL Books Online. It's the beginning of a pretty extensive maze, and how far you get into it depends on your ultimate goals and objectives.
所有这些都在 SQL 联机丛书中详细介绍。这是一个相当广泛的迷宫的开始,你进入它的程度取决于你的最终目标和目标。