3
votes

My ASP.NET project has an MDF database in App_Data folder.

The connection string is:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated Security=True;User Instance=True

It worked fine until I changed the project parameters to use "Local IIS Web server" instead of "Visual Studio Development Server".

The project now produces the following SqlException:

An attempt to attach an auto-named database for file C:\Users\Admin\documents\visual studio 2010\Projects\SL\SL\App_Data\MainDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

How to modify the connection string to make it work with IIS?

3

3 Answers

1
votes

You'll probably find this is file permissions (the error does state: "or specified file cannot be opened"). IIS runs as a limited-user by default, and it's unlikely to have access to the Administrators home directory.

Your three options are:

  1. Change the identity of the application pool in IIS to run as a user that does have access to that folder
  2. Move the MDF file to a location that IIS can access (and then check the permissions of the file)
  3. Change the permissions of the file (and possibly the directories above it) so that IIS can access the file.

Of these, number 2 is the most desirable. The other two reduce security somewhat - the first, by giving IIS free reign over a lot of the file system (and potentially system resources as well); whilst the third has the potential to open up a user's home directory to IIS.

1
votes

Since you are using Integrated Security, the user connecting has to map to a SQL Server login. When you changed to IIS, you switched the user and it may not map to a login in the DB. You could test this by specifying a username and password that are valid in your database.

For example:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;User Id=admin;Password=password;User Instance=True

0
votes

Make sure the user account your IIS process is running as has correct permissions to that file.