Table of Contents
When upgrading from MS SQL Server 2008 to MS SQL Server 2012, I decided to retain the data directories I was using in SQL 2008. I therefore did a backup and detached all my private databases, removed SQL 2008 and did a clean install of MS SQL 2012.
I logged into the new server and on trying to attach back any of my databases, I encountered the following error:
CREATE FILE encountered operating system error 5 (Access is denied) while attempting to open or create the physical file ...
There are two ways you can use to go around the error.
One is to close the current session of SQL Server Management Studio (SSMO) and open a new session of SSMO but right click while opening and select Run as Administrator. You can now attach the databases without errors. After attaching the databases, you can close the admin SSMO and open another one without admin rights.
The other method is by using sqlcmd utility which comes pre-installed with SQL Server. Open a windows command prompt as an administrator and run the sqlcmd with the -E flag as follows:
sqlcmd -E -S localhost -- If using an instance replace localhost with .<Instance Name> use master; GO CREATE DATABASE databasename ON (FILENAME='E:\Data\data.mdf') -- replace with your database and location FOR ATTACH; GO