Skip to content
30/05/2008 / dimarzionist

howto: Attach a database file to SQL Server Express

  1. Open the command prompt on the server.
  2. From the command prompt, connect to an instance of SQL Server 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. When connected, type the following commands:
    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 you want to attach, FileName is the path and file name 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. To verify that the database has been attached, type the following two commands:
    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, you should see the database name you provided in step 3.

(from here)

Notice: Don’t forget to run command prompt with administrative permissions.

Also, if you are interesting Microsoft has moved sample databases for SQL Server from MSDN to CodePlex, so now they available here. There is an MSI installation that by default will install Sample Database files into your SQL Server Data directory.

Here is some additional information about sample databases installation on Journey to SQL Authority with Pinal Dave

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: