ARTICLE

How to attach a database in SQL Server 2005

Posted by Raj Kumar Articles | Visual Basic 2010 February 03, 2009
This article describes how to attach database in SQL Server 2005.
 
Reader Level:

Here in this article, I will describe how to attach a database in SQL Server 2005.

One of the easiest way to distribute, copy or backup a database in SQL Server 2005 is to use attach and detach options.

A SQL Server database has two types of files. The first file is the main data file and it has extension of .MDF and the second file is the log file and it has an extension of .LDF. If you have the log file for a database, the easiest way to attach the database is to use the existing log file.

Follow these steps for attaching database:

  • To attach a database right click on the Database folder and select the Attach Database command to display the Attach Database dialog box then click on the Add button and use the resulting dialog box to select the .MDF file for the database. This should add both the data file and the log file for the database to the database details pane at the bottom of the dialog box and at last click OK to attach the database. 
  • If you want to attach a database that does not have a log file or if you want create a new log file for database, you can remove the log file for the database before you click on OK button. To do that select the log file in database details pane and click the Remove button. 
  • To detach a database right click on the database and select Tasks/Detach command. The click on the OK button.

The default directory for SQL Server 2005 database is:

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

Open Object Explorer in SQL Server and right click on Databases and select Attach menu item as you see in the below image.

untitled.JPG

Now, select a file option and use Browse button to browse your MDF and LDF files as seen in below image.

untitled1.JPG

That's it. Your database is attached now and ready to use.

The reverse operation of Attach is detach. The Detach option removes the database from the Databases list in the Object Explorer and saves the files to the given location.

Right click on the database name you would like to detach and select Detach menu item.

untitled2.JPG

Once a database is detached, you may distribute or back it up and once you're done, you may want to attach it again to start using it.

Hope this tip was useful for who are looking for attach and detach options in SQL Server 2005.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Example: DBCC TRACEON(1807) GO PRINT 'ATTACH FIRST' CREATE DATABASE TEST ON ( FILENAME = N'\\server\tmp\test.mdf' ), ( FILENAME = N'\\server\tmp\test_log.ldf' ) FOR ATTACH; GO PRINT 'Detach' exec sp_detach_db TEST GO The Service account has permissions on the network drive. The problem is that SQL Express need full rights because of changing the permissions when attaching the db files. First time the database could attached, but then the permissions are changed an the database could not attach again.

Posted by Raj Kumar Feb 20, 2011

thanks sir.

Posted by vikas kumar Feb 08, 2011

i want to know attach database over the network .It is sql qurey

Posted by thiri nwe Sep 27, 2010

Both are used to restore database we Use attach database when we have .mdf file to upload data into database while we use restore database when we have .bak file to upload data into database.

Posted by Raj Kumar Jun 07, 2010

thanks buddy

Posted by Rahul K May 26, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor