Welcome to Tech-Review.Org Sign in | Join | Help

.net_2.0

My coding blog entries. Typically will either be more complex coding examples or overcoming product issues / troubleshooting resolutions.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Having spent a good two hours troubleshooting the :

 

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file D:\Sites-Easy\Web\App_Data\LocalRepository.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Source Error: 

 

I ran across a ton suggestions and tips and this is the RIGHT ANSWER. (The Link that l read primarily to understand what the problem was and why not to use these suggestions: http://forums.microsoft.com/msdn/showpost.aspx?postid=98346&siteid=1&sb=0&d=1&at=7&ft=11&tf=0&pageid=4)...

 

Note: This fix may or may not work for XP as I only did this on Vista)

 

1. The issue with attaching to a mdf on the  file system under vista is that the normal ASP_Net account we were used to assigning  is kind of hidden even if you run as the Administrator and the UAC is turned off.  The actual issue truly is 1. logging into SQLExpress and 2. granting the right access..By default - SQLEXPRESS does not grant any schema or role to the user that installed it (even if you are the administrator).. see this screenshot...

 

 http://tech-review.org/photos/sample/images/3027/original.aspx

The typical recommended web.config connection string is this:

<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;Integrated Security=True;AttachDBFilename='|DataDirectory|MyData.mdf';User Instance=false"/>

 

 The integrated security is the issue because if you are logged in and SQLEXPRESS doesn't grant you any roles, then when it does it deal with tracking stats etc from the Master table (and using the tempdb for queries etc..), then it fails...because your account has no rights and therefore SQLEXPRESS just reports back that it is not able to attach without giving a real explicit error message.

 

After trying every single suggestion in the 5 page post - I realized that the issue was not because of files in the AppData directory (deleting those files would of required re-installing btw)...and it really had nothing to do with the integrated security.  My problem was two fold:

 

1. I didn't create the website structure from Visual Studio - I had manually configured IIS in Vista.

2. I am not using the Web Application Project structure and therefore I do not publish my website.

 

I am assuming that if I had adopted using VS to handle all of that that accessing file system mdf would not be an issue.  

 

So, my fix to the problem (and what I believe is the right way as it mimics the full blown SQL Server roles structure is:

 

1. Use Enterprise Manager to connect to ".\SQLEXPRESS

2. Drill down to security.  

3. Add new user, and grant it proper roles.

4. Attach to the file on the disk you want to grant the new user dbo access to. And add the user and assign the specific roles.

5. Modify the web.config and change the connection string to :

"data source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\LocalRepository.mdf;user instance=false;uid=appuser;pwd=userpassword" 

 

Now you will find you can use your application properly and it will attach to the database.  However, if the database does not exist you will get the aforementioned error.

 

Hope that solves someone else's problem dealing with SQLEXPRESS and trying to connect to the database...

 

 Additionally, use the SQL Configuration Manager to review whether SQLEXPRESS has Network Configuration enabled for the protocol you desire.  By default (just like SQL Server 2005 Standard and above) it is locked down and the only protocol enabled is Shared Memory).  Which is fine (and from a security standpoint proper and recommended).  However, if your MDF is on a shared network drive - the only way you can access it is by enabling TCPIP or NamedPipes on the network server that will store the mdf. 

 

 

 

 

 

 

Posted: Sunday, July 15, 2007 3:22 PM by Jody
Filed under:

Comments

Dan said:

Thanks for sharing your solution, I currently avoid using mssql express databases in mdf files by the pain caused me in the past.

I prefer to manage them like a normal sql server database, but I'll try your solution to see if the experience is better.

# July 17, 2007 11:36 AM
New Comments to this post are disabled