Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having trouble connecting an existing database to a new project. I open the new forms project (MovieList104) and attempt to attach an existing database. I keep getting the following error and cannot figure out why.

“Unable to open the physical file C:\Data Files 092015\DVD_List.mdf
Operating System Error 5: ‘5(access Denied)’
An attempt to attach an auto-named database for file D:\Data Files\DVD_List.mdf failed. A database with the same name exists or specified file cannot be opened or it is located on UNC Share”

I haven’t written any code yet so there is nothing to show.
I have no idea what is wrong. It’s the first time I’ve tried to attach this file, so after that I’m LOST! Hopefully someone can help me.
Posted
Updated 25-Sep-15 16:16pm
v2

Why are you trying to attach a database file if you are using SQL?
Just let SQL Server handle it, and connect to it via SQL.

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
Member 10376725 25-Sep-15 17:38pm    
Tried that, get the same error message. I shut down the computer, restarted it, ran VS under administrator and still get the same error message. At first I got an error message that stated the server I was calling didn't exist so in advanced settings I changed the server name to SQLEXPRESS2014 (the Server name on this computer) then I get the above message.
Member 10376725 25-Sep-15 18:24pm    
I don't know why I didn't notice this before, the error message refers tp a database named "C:\Data Files 092015\DVD_List.mdf" but the file name in the connection string is "D:\Data Files\DVD_List.mdf"(which is the correct file). At no time in this new project have I designated the file in C: which doesn't even exist anymore it was copied to D:\Data Files directory then deleted from the C: drive.
Member 10376725 wrote in commment to the solution 1:

I don't know why I didn't notice this before, the error message refers tp a database named "C:\Data Files 092015\DVD_List.mdf" but the file name in the connection string is "D:\Data Files\DVD_List.mdf"(which is the correct file). At no time in this new project have <<u>b>I designated the file in C: which doesn't even exist anymore it was copied to D:\Data Files directory then deleted from the C: drive.


Please, don't do that anymore! This method of moving database files is disallowed. Please, read this how to do that in proper way: Move Database Files[^]

Finally, follow this: How to: Connect to a Database[^]
 
Share this answer
 
Comments
Member 10376725 28-Sep-15 18:09pm    
Maciej Los; Okay, Okay, scolding accepted. I tried to move the database as the instructions say to move it in 'Move Database Files' but get this error:

Msg 5011, Level 14, State 5, Line 1
User does not have permission to alter database 'DVD_LIST', the database does not exist, or the database is not in a state that allows access checks.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

I ran SQL SERVER Management Studio in Administrator mode and get the same error. The weird thing is, the same Manager would allow me to back up the same database with no problem in either mode. I did manage to set the connection to the database through Server Explorer-Data Source and it works great but, I want to be sure I have good backups and know how to 'PROPERLY' move a database. Thank you for the assistance and if you have any other tidbits to help in this new problem it would be greatly appreciated.

darby

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900