Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

asp.net - An attempt to attach an auto-named database for file ....database1.mdf failed

I am getting the following error while debugging my visual studio 2010 website:

An attempt to attach an auto-named database for file C:Users...DesktopDpp2012NewApp_Datadppdatabase.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 C:Users...DesktopDpp2012NewApp_Datadppdatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is my connection string from my web.config:

<connectionStrings>
    <add name="ApplicationServices" 
         connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
         providerName="System.Data.SqlClient"/>
    <add name="ConnectionString" 
         connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|dppdatabase.mdf;Integrated Security=SSPI" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

And I access it from my website as:

Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

The stacktrace shows the error line as:

 Dim conn As New SqlConnection(connectionString)
 Dim dr As SqlDataReader
 conn.Open() 'This is the error line as per stacktrace

I have given the needed permissions to the above folder so it can not be the " or specified file cannot be opened" issue. If we look at all the posts related to the same error in this forum, clearly the profoundness of this error can be found out. However, none of the solutions solves my issue. Some of the resources which I have tried are:

  1. http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx
  2. http://blogs.msdn.com/b/webdevelopertips/archive/2010/05/06/tip-106-did-you-know-how-to-create-the-aspnetdb-mdf-file.aspx
  3. http://forums.asp.net/t/1033225.aspx

I eagerly await a solution.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had this problem also and it was a pain. I configured my connection string and managed to solve the problem. In the connection string I replaced the value |DataDirectory|dbfilename.mdf for AttachDbFilename property, with the path to the file. |DataDirectory| can only be used if the database file is in the App_Data folder inside same project.

So changing the AttachDbFilename property to the direct path of the mdf file, fixed my issue.

AttachDbFilename=C:MyAppAppDALdb.mdf

I hope this works for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...