I am trying to attach a database in SQL Server 2008 and I have .mdf file of database but I am getting an exception of
Attach database failed for Server 'Bilal-PC'. (Microsoft.SqlServer.Smo)
Unable to open the physical file "E:\Intelliscence\Backups\test (2011年07月19日)\Database\QBankDB.mdf". Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)
I searched the solution of this problem and found one solution of access rights of database file but database file has already full control access rights. Now I want to attach this database or want to get script of this database. But How?
I know this is a duplicate question but all the solutions given in answers are not working for me that's why I am posting this question.
Please help me.
-
I know the tags are unrelated but C# and asp.net developer use the SQL ServerBillz– Billz2012年06月05日 17:41:54 +00:00Commented Jun 5, 2012 at 17:41
-
Are the files marked read only?RThomas– RThomas2012年06月05日 17:46:27 +00:00Commented Jun 5, 2012 at 17:46
-
@RThomas good point, or already attached to another SQL Server instance.Aaron Bertrand– Aaron Bertrand2012年06月05日 17:59:41 +00:00Commented Jun 5, 2012 at 17:59
7 Answers 7
Try to run the Management Studio as Administrator and attach the database.
Right click on Management Studio Properties and Run As Administrator.
Hope it helps you.....
4 Comments
The error message seems pretty straightforward: Access is denied. You need to make sure the SQL Server service account has adequate permissions on E:\Intelliscence\Backups\QBank (2011年07月19日)\Database,円 or move the MDF/LDF files for the data/log folders for the instance (where this shouldn't be a problem).
2 Comments
u should edit security of .mdf & .ldf files to full control like this pic enter image description here
Comments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Instructions for attaching database to the SQL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run "SQL server 2014 management studio".
Login to \SQLEXPRESS.
Right click on the Databases folder and select "Attach".
In the opened UI select Add.
Browse to the folder where the .mdf and .ldf files are located.
Select the .mdf file. Notice: The .mdf and .ldf files you've selected will be deleted, so make sure you make a copy of those files beforehand.
Click on the "Attach As" tab and type the new database name.
Make sure the "Current File Path" are correct for the ldf and the mdf files.
Click ok.
Comments
Run the Management Studio as Administrator and connect server with windows authentication.
Try to attach the database.
I have done with this.....
Comments
I think this has nothing to do with permission, I exactly don't know the entire scenario, but for example, if you are trying to attach an mdf from SQL server 2008 to SQL server 14 or something else, just copy the MDF and LDF file from C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA and place them in the C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA and then try using the regular attach method, and you should be fine
Comments
It might be because of
- Default Location of SQL Data Files Has Changed
- SQL Server Resets File Permissions when Database is Detached
Try any of the following.
- Run SQL Server Management Studio as an
Administrator. (right click->run as administrator). - The account that I was using had full control NTFS permissions to the mdf and ldf files. However, it had those permissions through group membership (the Local Administrators group had permissions, and my account is a member of local admins). So Give Full Access for *.mdf / *.ldf files.
Please refer here for more information.