How to attach a database file to SQL Server Express

Developers can attach a database file to an instance of SQL Server 2005 Express Edition by using the sqlcmd tool. They have to follow the next steps:

1. Open the command prompt on the server.

2. Connect to an instance of SQL Server 2005 Express Edition, from the command prompt, by using the following sqlcmd command:

sqlcmd -S Server\Instance

 

Where Server is the name of the computer and Instance is the name of the instance

3. Type the following command, when they are connected:

USE [master]

GO

CREATE DATABASE [database_name] ON

( FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\<database name>.mdf’ ),

( FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\<database name>.ldf’ )

FOR ATTACH ;

GO

 

Where database_name is the name of the database they want to attach, FileName is the path and filename of the database file and the log file, and FOR ATTACH specifies that the database is created by attaching an existing set of operating system files.

4. Type the following commands to verify that the database has been attached:

select name from sys.databases

go

5.The sqlcmd tool displays the names of all databases attached to this instance of SQL Server Express. In the list, Developers should see the database name they provided in step 3.