2

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

3
  • What error message do you receive? Do you get a "Timeout" message (suggesting that you don't have th instance name right), a "Login Failed" message (bad login details), or a "Default database not found" message? Or something else? Please provide some more details. Commented Sep 7, 2010 at 20:28
  • failed to generate a user instance of sql server due to a failure in starting the process for the user instance. the connection will be closed Commented Sep 7, 2010 at 20:30
  • Data Source=.\SQLEXPRESS;AttachDbFilename=C:\SourceCode\NcrCarDatabase\NcrCarDatabase.Web\App_Data\NcrCarDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True Commented Sep 7, 2010 at 20:32

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

0

Give Security to Full control for your application Folder and .mdf, .ldf files. Write click on the folder which contains .mdf,.ldf files

  1. Go to property
  2. click security in that
  3. Click Full Control in allow.

and them proceed your process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.