#PSTip Drop all Active Connections of SQL Database
Note: This tip requires PowerShell 2.0 or above.
In an earlier tip, we looked at how we can retrieve the active connection count for a SQL database. In today’s tip we will look at how we can drop all the active connections before we can perform a detach database operation.
For this purpose, we will use the KillAllProcesses() method of the Server SMO.
Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" $smo = New-Object Microsoft.SqlServer.Management.Smo.Server $env:COMPUTERNAME $smo.KillAllProcesses('MyDB')
This code snippet will help you drop all active database connections of a given SQL database.
Share on: