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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With