Attach Detach Database in SQL Server using TSQL master.dbo.sp_detach_db and master.dbo.sp_attach_db

Below is the T-SQL script way to attach and Detach SQL Server Database

--Detach Database
USE master
GO
ALTER DATABASE [AdventureWorks2008R2] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
EXEC master.dbo.sp_detach_db @dbname = N'AdventureWorks2008R2'
GO

--Attach Database
USE [master]
GO
EXEC master.dbo.sp_attach_db @dbname = N'AdventureWorks2008R2',
    @filename1 = N'D:\MSSQLSERVER\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_Data.mdf',
    @filename2 = N'D:\MSSQLSERVER\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data_Log\AdventureWorks2008R2_log.LDF';
    --If you have more files add it here
GO