error in attaching.mdf file - sql-server

When I try to attach a .mdf file in Management Studio to open it gives me the following error:
An exception occurred while executing a Transact-SQL statement or batch.
(Microsoft.SqlServer.ConnectionInfo)
Unable to open the physical file "C:\DBClass\Sales.mdf".
Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)
What is the problem and how do I solve it?

Having just had the same error message, the solution for me was to run SQL Server Management Studio as Administrator (right click > Run as Administrator) and then attach the .mdf file as normal. You can then either continue with Administrator privileges or shut down Management Studio and launch again as a regular user.

Are you sure it isn't attached in another instance of SQL Server?
EDIT:
Possible issues are:
File is being held open by another process (like another instance of SQL Server)
File is corrupt
File is compressed (I encountered this a lot in XP)
Actual permissions issue with the folder/file
Are you sure the MDF file is good? Has it worked on another machine/instance in the past?

Have you checked that the mdf file isn't read-only and that your user account has sufficient privileges to access the file?

1st you remove log file from folder there .mdf file place
then set folder path
i set mine path
CREATE DATABASE TestDb ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')
FOR ATTACH_REBUILD_LOG
GO

Related

Privileges right error when re-attaching a detached database

I detached a SQL Server database for moving the files (both data and log) to another computer. When I tried to re-attach the files, I get this message:
C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA
Cannot access the specified path or file on the server. Verify that you have the necessary security privileges that the path or file exists.
I added "full control right" to my security privileges on the folder C:\Program Files\Microsoft SQL Server. However it failed for some subfolders.
Then I tried adding "everyone" and assigned it with "full control right" without any fails. But the same message still persisted.
Can anybody help me?
Verify that the path is correct, as it changes from version to version. Then add rights to the service account used by SQL Server. Also make sure to be checking the correct computer, meaning that you should be handling the files, folders and permissions in the server not your client computer.

Why does SQL Server Mgmt Studio 17.9 save MDF to C:\Users? Error: Msg 5123, Level 16, State 1, Line 1 CREATE FILE access denied

I get the error
Msg 5123, Level 16, State 1, Line 1
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file
after launching a localDb server and trying to run a create database query off it. I am very new to SQL Server and need to do a tutorial for an internship I am starting.
I created a localdb instance in my onedrive directory. At least I think I did because that's the directory I navigated to when I ran the
sqllocaldb c "LocalDBdemo" 14.0.1000.169
command on my command prompt (I use Hyper):
So why is it that SQL Server Management Studio v17.9.1 is trying to create an .MDF file in my C:\users\ directory?
It wants to put it in C:\users. Why not put it into the directory I created in Onedrive?
A solution I saw which I did was to make my c:\users directory accessible to everyone under permissions. I didn't want to do it but I did and it worked. But why do I even need to do this when I am running the program from within my windows user admin account which is the only account on my computer? Also how do I get SQL Server Mgmt Studio to create my .MDF databases wherever I choose?
Just specify an appropriate location when you create a database:
CREATE DATABASE Customer
ON (NAME='Customer_Data', FILENAME='C:\AppropriatePath\Customer.mdf')
LOG ON (NAME='Customer_Log', FILENAME='C:\AppropriatePath\Customer.ldf');

Attach sql server 2012 error, Unable to open the physical file

I got message box
Unable to open the physical file "C:\Project Basdat\DAYCARE.mdf".
Operating system error 5: "5(failed to retrieve text for this error.
Reason: 15105)". (Microsoft SQL Server, Error: 5120)
when trying to attach database file that I made from another pc.
I use Windows 10.
Help :(
The link I posted previously is broken so I removed it. If you run SQL Server Management Studio as administrator it should work just fine.
This is permission issue on folder.
GO to the folder where you have stored mdf file.
select file-> Right click -> click on .
give full permissions to file for logged in user Security and (NT Service\MSSQLSERVER).
When I faced this issue, I tried
Running SQL Management Studio as Administrator
Giving full permissions to the file for logged in user
Still the error persisted.
The reason for this error might be that you copied your program from a source which Windows considers non-trusted.
You can unblock the assembly by clicking "Unblock" after you right-click and select properties on the file.

SQL Server Error 5120 - failed to attach .mdf file

While attaching my .mdf file on my SQL Server database I am getting below error message:
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file
"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA'
Operating system error 5: "5(Access denied.)".
I don't have my .ldf file of associated database. How can I attach my .mdf file without .ldf file? I am using SQL Server 2014.
GO to the folder where you have stored MDF file. You can either do this to the entire folder or the MDF/LDF individually. Select file/folder-> Right click ->click on "Security". Then "Edit'. If you dont have OWNER RIGHTS listed Click ADD and search for it, click OK.
And then give Full Control to user OWNER RIGHTS.
permission for mdf file
That error is telling you that the account that is running the SQL Server service doesn't have permission to the mdf file. Fix the permissions and it should attach just fine.
Pro tip: pictures of text aren't as useful as the actual text. :)
Run SQL Server Management Studio as Administrator to attach the .MDF file. After that, no need to run SSMS as Administrator any more.
Put your data file into the folder where you made permissions
https://dbamohsin.wordpress.com/2009/06/03/attaching-database-unable-to-open-physical-file-access-is-denied/

Can't connect to my own MDF file. Cannot open user default database. Login failed.Login failed for user... and other errors

SQLSEXPRESS service IS running!!
I have I program I wrote some time ago and it works. It was written in MS Visual Studio with local MDF file.
How is it possible to edit this file from Microsoft SQL Server Management Studio? I don't see this file in the list of globally connected databases.
If I try to attach this file with Visual Studio Wizard, I get message that such database already exists, but when I set arbitrary logical name, I get sharing violation.
How to reach MDF file?
EDIT 1
Now I found that my program also stopped to work.
The summary of results of different attempts to reach MDF file:
Program itself System.Data.SqlClient.SqlException (0x80131904): Cannot open user default database. Login failed. Login failed for user <username>. (windows user)
Visual Studio, Data source = Microsoft SQL Server, Logical Name = "" Error message: An attempt to attach an auto-named database for file
<filename> failed. A database with the same name exists, or
specified file cannot be opened, or it is located on UNC share.
Visual Studio, Data source = Microsoft SQL Server, Logical Name = SOMENAME Error message Unable to open the physical file <filename>. Operating system error 32: "32(The process cannot access the file because it is being used by another process )".
Cannot attach the file <filename> as database ‘SOMENAME’.
Visual Studio, Data source = Microsoft SQL Server Database File, Windows Authentication Error message: Cannot open user default database. Login failed. Login failed for user <username>.
Visual Studio, Data source = Microsoft SQL Server Database File, SQL Server Authentication Error message: Failed to generate a user instance of SQL Server. Only an integrated connection can generate a user instance. The connection will be closed.
Microsoft SQL Server Management Studio, Attach Database Error message: Unable to open the physical file <filename>. Operating system error 32: "32(The process cannot access the file because it is being used by another process)". (Microsoft SQL Server, Error: 5120)
MDF files are not intended to be used directly.
You may use them only through and with the help of SQL Server Engine.
Try this:
1- Create a database with the same name to mdf file.
2- Stop sql service
3- Go to the Data Carpet where is all mdf files and ldf , copy your original mdf file y replace.
4- Start the service
5- See in the management studio if the database still online there you go.
A .MDF database cannot be "opened".
It must be attached to an existing (and running) SQL Server instance.
If you have SQL Server Management Studio running, open a connection, and right click on databases. Choose "Attach" and point to the .MDF file. This will (attempt to) attach the database to the master database. If it works, the database will be visible in SSMS.

Resources