Detaching a database
In this recipe we will detach a database programmatically.
Getting ready
For purposes of this
recipe, let's create a database called TestDB
. Open up SQL Server Management Studio and run the following code:
CREATE DATABASE [TestDB] CONTAINMENT = NONE ON PRIMARY ( NAME = N'TestDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TestDB.mdf' , SIZE = 4096KB , FILEGROWTH = 1024KB ), FILEGROUP [FG1] ( NAME = N'data1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\data1.ndf' , SIZE = 4096KB , FILEGROWTH = 1024KB ), FILEGROUP [FG2] ( NAME = N'data2', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\data2.ndf' , SIZE = 4096KB , FILEGROWTH = 1024KB ) LOG ON ( NAME = N'TestDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TestDB_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%) GO
How to do it...
Open the PowerShell console by going...