Sql-server – Cannot attach the AdventureWorks database

sql server

I am trying to attach the AdventureWorks database with the following query:

CREATE DATABASE AdventureWorks2012
ON (FILENAME = 'C:\AdventureWorks2012_Data.mdf')
FOR ATTACH_REBUILD_LOG ;

But I get the following error:

Create file encountered operating system error 5 (access is denied.) while attempting to open the physical file blah

Note: I am using Windows auth mode.

Any pointers as to what to do?

Best Answer

SQL Server service account don't have permission in the drive you are using to create database.Don't put you database in the root of drive. It would be problematic if you have UAC enabled, Create a folder in drive. Like D:\Data

Check this http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.

Make sure that the SQL Server Service Account has read/Write permission to the folder where database file is kept or use the instance default folder of Data/Log where it will already have such permissions.