Test Environment: MS Server 2008
MS SQL Server – An error occurred when attaching the database – AdvenureWorks database
- Make sure you enabled database instance to use FILESTREAM as instructed here: http://msdn.microsoft.com/en-us/library/cc645923.aspx
- If you downloaded Adventureworks from codeplex, make sure you have set the right user permissions to these files
- Copy AdventureWorks2008_Data.mdf and AdventureWorks2008_Log.ldf to ‘C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\’. If you see a folder called ‘Documents’ in the downloaded files set, then copy this folder also to ‘C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\’
- In the management studio, open new query window and execute the below code: [Change the files path accordingly]
USE [master] GO CREATE DATABASE [Adventureworks] ON ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Data.mdf' ), ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Log.ldf' ) FOR ATTACH GO if not exists (select name from master.sys.databases sd where name = N'Adventureworks' and SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) EXEC [Adventureworks].dbo.sp_changedbowner @loginame=N'sa', @map=false GO
OR you can attach a database using MS:
- In SQL Server Management Studio Object Explorer, connect to an instance of the Microsoft SQL Server Database Engine, and then expand that instance.
- Right click Databases, then Tasks, and then click Attach.
- In the Attach Databases dialog box, to specify the database to be attached, click Add; and in the Locate Database Files dialog box, select the disk drive where the database resides and expand the directory tree to find and select the .mdf file of the database; for example: C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008_Data.mdf
- Optional : Specify a different name, change the ownership
- When you are ready to attach the database, click OK.