How to attach a SQL database created under a different account - sql-server

I created several SQL databases on an external drive using the local admin account on my PC. This PC was then recalled and rebuilt by Head Office.
I have tried and failed to attach these databases to a new install of SSMS.
All of this was done using SQL Server 2016 Developer Edition.
I can created new DBS on the external drive.
I have given my local admin account and the SQL NT Service account (including the SQL Agent one) full access to the folders, the .MDF and the .LDF files.
The error is 'Create file encountered operating system error 5(access is denied.) while attempting to open or create the physical file 'D:|SQLDATA\TESTDB.mdf.' (Microsoft SQL Server, Error: 5123)
Could it be because I didn't detach the databases from the old SQL Server install, which no longer exists?
If anyone can help, I'll be very grateful.

Run Sql as administrator.
or
Right click on file. Propetries > Security.
change the security permissions of ldf , mdf file to full permissions.

Related

MSSQL Restoring database giving error that unable to open physical file and operating system error 0 (null), I am unable to restore this data base

I am trying to restore this database from
https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0
but getting this below error. Help on this will much more appreciated.
The following Github issue suggests that the WorldWideImporters sample database requires the FILESTREAM capability: Restore database backup onto mounted directory with FILESTREAM #122
Error: Unable to open the physical file "C:\Temp\WideWorldImporters_InMemory_Data_1" Operating system error 0: "(null)"
...
nocentino commented on 6 Jan 2021
Hi there, Filestream is not supported in SQL Server on Linux and Docker containers. Check out this link for more details - https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-editions-and-components-2019?view=sql-server-ver15#Unsupported
From your screen shot it appears that you are using SQL Server Express's LocalDB which also does not support FILESTREAM, ref: SQL Server Express LocalDB:
Restrictions
LocalDB cannot be managed remotely via SQL Management Studio.
LocalDB cannot be a merge replication subscriber.
LocalDB does not support FILESTREAM.
LocalDB only allows local queues for Service Broker.
An instance of LocalDB owned by the built-in accounts such as NT AUTHORITY\SYSTEM can have manageability issues due to Windows file system redirection. Instead use a normal Windows account as the owner.

Loading .mdf/.mdv file into SQL Server Management Studio

I downloaded SQL Server Management Studio to my laptop at home. I don't have a database as it is my personal PC. Is it possible to connect to a .mdf file and do SQL querying in that ?
Let's say someone gives you an MDF file of which that's the only database file other than the log file. Can you "connect" to it?
Yes...
Let's say the .MDF file is here: D:\SQL\MSSQL12.MSSQLSERVER\MSSQL\DATA\DatabaseMDF.mdf
In SSMS, connect object explorer to your SQL Server. Right click the Databases folder, and select Attach Database. Add the file, D:\SQL\MSSQL12.MSSQLSERVER\MSSQL\DATA\DatabaseMDF.mdf. Verify that you have legal paths in the database details sub pane under the Databases to attach sub pane. Also, in the database details sub pane, remove the log file you do not have. Click OK... and you have the DB available.
You will need SQL Server installed and running as well. Just SSMS (SQL Server Management Studio isn't enough). You can download from here...
https://www.microsoft.com/en-us/download/details.aspx?id=42299
Windows 7
https://www.microsoft.com/en-us/download/details.aspx?id=55994
Windows 10

Access denied to Sql Server mdf file from Visual Studio 2013 data connections

After a fresh install of Windows 8.1 and VS 2013, I noticed that adding a data connection from VS to a Sql server database file didn't work. I tried an existing mdf file.
I installed VS 2013 ultimate and I assume that this installattion includes Sql server tools to open database files (I included that during installation). But I did not install any Sql server edition or express edition.
Error: "unable to open the physical file", "access denied" and also: "an atttempt to attach an auto-named database for file ...mdb failed" and "a database with the same name exists, or specified file cannot be opened, or it is located on UNC share".
Well my account is an administrative local account and I run VS in administrative mode. I checked the security of the files and the group 'administrators' has all rights.
Also the mdf file is not corrupt or so and on the local hard drive.
you would have to attach the database to an sql instance, can be done in your connection string

directory lookup for the file failed with the operating system error 5(access is denied.)

I had SQL Server 2008 R2. I uninstalled it and installed SQL Server 2012 - now I can't attach database or back up database because I have error:
directory lookup for the file failed with the operating system error 5(access is denied.)
What should I do? :(
Do check your directory path at which Backup and Restore taking place. (some folder might be deleted).
Also solution would be you can backup using scripts and there you could avoid this error by mentioning the path to backup or restore database.
Refer to
http://social.msdn.microsoft.com/Forums/en-US/sqlkjmanageability/thread/1d044ccd-9c16-4a60-819d-cf7f9125384c
SQL Server 2012 runs with your user account , so if you don't have read and right access, you will get this error so give your account administrator rights or proper rights needed to perform your task.

sql server mdf file database attachment

I'm having a bear of a time getting visual studio 2010 (ultimate I think) to properly attach to my database. It was moved from it's original spot to #MYAPP#/#MYAPP#.Web/App_Data/#MDF_FILE#.mdf. I have three instances of SQL Server running on this machine. I have tried to replace the old mdf file with my new one and cannot get the connectionstring right for it.
What I'm really trying to do is to just open a DB instance and run a DB create script. Then I can have a DB that was generated via my edmx (generate database from model) in Silverlight business application (c#)
Right now, when I go to Server Explorer in VS, choose add new connection, choose MS SQL Server Database File (SqlClient), choose my file location (app_data directory), use Windows authentication, and hit the Test Connection button, I get the following error:
Unable to open the physical file "". Operating system error 5: "5(Access Denied.)". An attempt to attach to an auto-named database for file"" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
The mdf file was created on the same machine by
Connecting to (local) in SQL Server management studio.
Getting a new query.
Pasting in the SQL from the generated ddl file.
Adding CREATE DATABASE [NcrCarDatabase]; GO; before the pasted SQL and
Executing the query.
I then
Disconnected from the DB in Management Studio.
Closed Management Studio
Navigated to the DATA directory for that instance
Copied the mdf and ldf files to my application's app_data folder.
I am now trying to connect to the same file inside visual studio.
I hope that gives more clarity to my problems :).
Connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\SourceCode\NcrCarDatabase\NcrCarDatabase.Web\App_Data\NcrCarDatabase.mdf;Integrated
Security=True;Connect Timeout=30;User
Instance=True
I was getting "access denied" until I ran Visual Studio as an administrator. I then had to detach the database from MSSMS before I could get it into Visual Studio.
You mentioned that your MDF file was moved. Was the MDF properly attached to SQL Express after the move? See: How to: Attach a Database File to SQL Server Express.
Access denied might well mean that you already have an active instance that has opened the file prior to you doing it - and if you have three active instances, it is likely one of them that is the culprit.
You can try to connect to the different instances with Management Studio and see if you can find the base there. If yes, the detach it from the instance and re-try from Visual Studio. Or, if possible, try to shut down all three instances and then re-try from Visual Studio. If that works, restart the instances one by one to determine which of them is trying to get the file.
I've previously answered a similar question that might help.
Your Sql server service runs under a given account. You should determine the account through the service properties then change the db file security access to full access for that account.
Give Security to Full control for your application Folder and .mdf, .ldf files. Write click on the folder which contains .mdf,.ldf files
Go to property
click security in that
Click Full Control in allow.
and them proceed your process.

Resources