Sql Server Express Connection Problem

While creating some samples for the upcoming 2007 product, I came across to Sql Server Express connection problem as in the following:

Exception information
Exception type: SqlException
Exception message: Unable to open the physical file “D:\SampleSite\App_Data\SqlSample2.mdf”. Operating system error 5: “5(Access is denied).

An attempt to attach an auto-named database for file D:\SampleSite\App_Data\SqlSample2.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

This is very strange and annoying since the database creation is done in VS 2005 and is a very simple and straightforward thing. But this error keep appearing. It took me several minutes to figure out the problem after several trial and error.

If you came across this problem, the solution is very easy:

  1. Open up Windows Explorer and go to the App_Data folder containing the database file.
  2. Open up Property | Security. Then add “Network Service” and grant it full access to the folder and all files inside.

For some reasons, you might need to add “SYSTEM” and “ASPNET” account as well under some circumstances (the difference is caused by the web project mode — whether it is FileSystem or IIS).

However, for our 2007 product samples and tutorials, we have decided to use FileSystem web project mode as it has less problems, easier to configure, and no dependency to IIS.

Hope this helps.

Advertisements

One thought on “Sql Server Express Connection Problem

  1. Scott

    Oh how I wish I found this article first! We’ve been struggling with this problem all day, and this solution worked like a charm. Thanks for posting it!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s