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

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

Related

Cannot open backup device Operating System error 5 (Access is denied)

I am trying to restore .bak file on SQL Server 2014, but it is not working. Initially, I tried:
On SQL Server 2014, right click on Databases -> Restore Database
From Source, click on Device and chose the path to the .bak file
However, this did not work and nothing happened. When I was reading old SO posts to find a solution for this, someone had said I could use SQL command for it. So I tried the following command:
RESTORE DATABASE Student
FROM DISK = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.MSSQLSERVER2014\MSSQL\Backup\Student.bak'
However, this gives
Cannot open backup device ''C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.MSSQLSERVER2014\MSSQL\Backup\Student.bak'. Operating system error 5 (Access is denied)
RESTORE DATABASE is terminating abnormally
When I SO'ed this error, I found some posts with the same issue. Someone said:
On SQL Server 2014, right click on Databases -> Restore Database
Click on Files
Fill the checkbox Relocate all files to folder
will fix the issue, and this answer got many up-votes. However, on my SQL Server, this checkbox is grayed out and I cannot even check/uncheck it.
I also tried a way that many had mentioned:
Open SQL Server 2014 Configuration Manager
Change Log On As of SQL Server (MSSQLSERVER2014) to LocalSystem
This also did not work.
I am at a loss what to do to fix this problem. This problem started happening after I uninstalled and reinstalled SQL Server 2014. I had an installer, but I mistakenly selected Evaluation Mode about a month ago when I first installed it, and today I got an error message saying:
Evaluation period has expired.
So I uninstalled SQL Server 2014 and reinstalled it with EXPRESS Mode this time. Upon installation I got an error and I had to change the default name from MSSQLSERVER to MSSQLSERVER2014 because I already had the folder of that name from the previous installation. So I am trying to restore the .bak file in MSSQLSERVER2014.

How to attach a SQL database created under a different account

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.

SQL Server Management Studio error 948

I tried to attach a database (.mdf) file and I got this error:
Attach database failed for Server '...\SQLINSTANCE'. (Microsoft.SqlServer.Smo)
Additional information:
An exception occured while executing a Transact-SQL statement or batch.
(Microfost.SqlServer.ConnectionInfo)
The database 'DBName' cannot be opened because it is version 782. This server supports version 706 or earlier. A downgrade path is not supported.
Could not open new database 'DBName'. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)
P.S. I have installed SQL Server 2012 Express, and database 'DBName' has been made in Visual Studio 2012.
If you have source control
Rollback the mdf file
Rollback the idf file (log file) to the same version
I had a similar problem and this solution fixed is.
If you do not have source control and you are using Code First
Backup the mdf and idf files
Delete the mdf and idf files in the solution explorer
Create a new database from project->add new item->data->SQL server database
In the package manager console write "Enable-migrations" then "Add-migration init" then "Update-database" to create your tables in the database.
For me, if things got too complicated, I would just recreate the project and copy the code :)

SqlDataAdapter object in Visual Studio 2010

I am running Visual Studio 2010 and SQL Server 2008 Express editions. I have created a database in SQL called Hangman.mdf. Then in Visual Studio I drag the SqlDataAdapter object to my form and it brings up the wizard, in the wizard under data source I select the Microsoft SQL Server Database File (SqlClient) and then browse to my Hangman.mdf file. When I click on test connection it throws out this error:
Unable to open physical file "C:\XXX\Hangman.mdf". Operating system error 5: "5(Failed to retrieve text for this error. Reason: 15105)" An attempt to attach an auto named database for file C:\XXX\Hangman.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I am running Windows 8, I have opened up Visual Studio and SQL Server as Administrator.
I would appreciate any help.
Thanks

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