Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach .mdf file to .SQLEXPRESS

Tried this in as connection string

connectionString="Server=.\SQLEXPRESS;
    AttachDbFilename=E:\Database\dnn49.mdf;Database=dnn49; 
    Trusted_Connection=Yes;" 

but i get an error

Unable to open the physical file "E:\Database\dnn49.mdf". Operating system error 5: "5(Access is denied.)". Cannot attach the file 'E:\Database\dnn49.mdf' as database 'dnn49'.

What gives?

like image 560
epitka Avatar asked Sep 20 '09 01:09

epitka


People also ask

How do I install .MDF and .ldf files?

Run SQL Server management studio as an administrator and attach the database. Explicitly grant full control access to the MDF file and LDF file of the database. To do that, Right-click the database files Select the security tab select the appropriate user and grant full control to the user.

How do you attach a file to a database?

To attach the database, right click on Databases and select Attach... on the SQL Server instance where you want to attach the database. The following screen opens. Click on the Add button to find the mdf file that you want to attach, select the file and click OK.

Can you attach MDF without LDF?

Attach MDF File Without LDF file by using T-SQL script: You can also run a TSQL Script on SQL Query to restore MDF database in SQL Server and recreate your transaction log file. Where, testdb is the name of your database. Now you can check your database in the database folder.


2 Answers

If you're attaching from SQL Server Management Studio, it also works to run SSMS as an administrator. This then can take advantage of the fact that the Administrators group has full control over the file. Another much more rare circumstance is that you'll get this Access Denied error because the database is already attached. This is probably more obvious unless you have multiple instances of SQL Server running and you already attached it to another instance possibly.

like image 112
Jason Roth Avatar answered Oct 15 '22 05:10

Jason Roth


You must check the SQL Server Service account has modify permissions over the physical file "dnn49.mdf" , and modify permissions also over the folder that contains the file.

like image 42
RRUZ Avatar answered Oct 15 '22 05:10

RRUZ