I want to close the existing connections to an MS SQL Server so that I can do a restore on that database programatically.
Right-click on a database in SSMS and choose delete. In the dialog, check the checkbox for "Close existing connections." Click the Script button at the top of the dialog.
If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.
Right-click the server in Object Explorer and select 'Activity Monitor'. When this opens, expand the Processes group. Now use the drop-down to filter the results by database name. Kill off the server connections by selecting the right-click 'Kill Process' option.
This should disconnect everyone else, and leave you as the only user:
alter database YourDb set single_user with rollback immediate
Note: Don't forget
alter database YourDb set MULTI_USER
after you're done!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With