locked
An attempt to attach an auto-named database for file ... failed RRS feed

  • Question

  • User764372941 posted

    When I attempt to run the Time Tracker Starter kit I receive the following error:

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

    I checked SQL Express and I did originally have a database named aspnetdb that was installed by the QuickStarts. I have subsequently detached that database and moved the data and log files from SQL Express's data folder, however I still get the same error.

    I've opened mdf file in visual studio and verified that it did indeed contain data and was not corrupt.  The directory is not in a UNC Share.  I've granted full security access on the WebSites directory to the "Everyone" user.

    I'm running Windows 2003 Standard Edition, using Visual Studio Team Suite, running under beta 2 of the Framework.

    I imagine that I could attach the mdf in Sql Express and then rewrite the ConnectionString to point to that, but I'd like to get it to work the way it was intended.  I've attached the Connection String section as well as the stack trace below. Does anyone have any ideas about how to fix this issue?

    <connectionStrings>
     <add name="aspnet_staterKits_Test_TimeTracker" connectionString="Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
     <remove name="LocalSqlServer"/>
     <add name="LocalSqlServer"connectionString="Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
    </connectionStrings>


    [SqlException (0x80131904): An attempt to attach an auto-named database for file C:\WebSites\Test_TimeTracker\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.]
       System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +684979
       System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +207
       System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1751
       System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +32
       System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +601
       System.Data.SqlClient.SqlInternalConnectionTds..ctor(SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +159
       System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +346
       System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28
       System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +445
       System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
       System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +304
       System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +85
       System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
       System.Data.SqlClient.SqlConnection.Open() +111
       System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +126
       System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +226
       System.Web.Security.SqlRoleProvider.RoleExists(String roleName) +467
       System.Web.Security.Roles.RoleExists(String roleName) +196
       ASP.Global_asax.Application_Start(Object sender, EventArgs e) in c:\WebSites\Test_TimeTracker\Global.asax:13

    -Rich

    Friday, April 22, 2005 2:47 PM

Answers

  • User764372941 posted

    I figured it out.  I took a look at the connection string on the personal web site starter kit which is similar in the way that it connects to the database.

    Apparently the Time Tracker Starter kit is missing the following option from its connections string.

    "User Instance=True;"

    Once I added this option to the Connection String everything works fine.  So the new connectionstrings look like this.

      <connectionStrings>
        <add name="aspnet_staterKits_Test_TimeTracker"
             connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
        <remove name="LocalSqlServer"/>
        <add name="LocalSqlServer"
             connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
      </connectionStrings>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 22, 2005 4:00 PM

All replies

  • User764372941 posted

    I figured it out.  I took a look at the connection string on the personal web site starter kit which is similar in the way that it connects to the database.

    Apparently the Time Tracker Starter kit is missing the following option from its connections string.

    "User Instance=True;"

    Once I added this option to the Connection String everything works fine.  So the new connectionstrings look like this.

      <connectionStrings>
        <add name="aspnet_staterKits_Test_TimeTracker"
             connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
        <remove name="LocalSqlServer"/>
        <add name="LocalSqlServer"
             connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
      </connectionStrings>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 22, 2005 4:00 PM
  • User994143857 posted

    Thanks for that Rich!

    That was one of the problems I was having when trying to run the app in my local IIS. After I changed the connection string, I got the same error message again, but this time with a different stack trace.

    Then I read your post again, and went to look at my databases with SQL Express Manager, and noticed that aspnetdb was already running in it. I deleted the aspnetdb,, reinstalled blank mdf files for it and the timetracker db, changed the permissions so that "Everybody" had full control to them, and then tested the app. I can now use it!

    Can anyone tell me what the user instance=true part of the connection string does?
    Monday, May 2, 2005 6:18 PM
  • User994143857 posted
    Replying to myself :-)

    User Instance = true is described in this post on the MS news groups:
    http://communities.microsoft.com/newsgroups/previewFrame.asp?ICP=sqlserver2005&sLCID=us&sgroupURL=microsoft.private.sqlserver2005.express&sMessageID=%253CuQCkdV5UFHA.2092@CPMSFTNGSA04.privatenews.microsoft.com%253E

    Sunday, May 8, 2005 11:22 AM
  • User-2039023399 posted
    doesnt work for me.

    <connectionStrings>
            <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>


    my User Instance=True is there by default but still get the UNC error thingy.


    Thursday, March 30, 2006 12:26 AM
  • User1778888661 posted

    Hi,

    The only times I have received this message have been when, after testing on my local server with it working perfectly, I have moved the application onto a remote test server and placed the application in a subfolder of the remote site.

    This always generates this message.

    After the data has been moved to the correct location (as it would be on a live server) or the test server location is identified in the web.config file then the problem has been resolved.

    In each case the relevant part of the message was "or specified file cannot be opened" as it is being told to look in the wrong location.

    Hope this helps you.

     

    Friday, September 15, 2006 4:21 AM
  • User-905044829 posted

    I had the same problem with my website freehosting1.net

    What i did to resolve it is to add a 'database' property to my connection string and delete the log file.

    But anyway i would like to note that sql server has alot of trouble giving you the right instructions to solve the probems,

    i mean the error messages gives u some info about it, but by far not enough to resolve it easily. 

    Wednesday, September 20, 2006 6:51 PM
  • User805391953 posted

    I have the same error after a current failure(my pc shat down suddenly)...my connection string is ok and I don;t know what's going wrong...

    Any Ideas?

    Thanks!!!

     

    Friday, February 2, 2007 3:40 AM
  • User113421904 posted

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    I have the same error after a current failure(my pc shat down suddenly)...my connection string is ok and I don’t know what's going wrong...<o:p></o:p>

    Any Ideas?<o:p></o:p>

    Thanks!!!<o:p></o:p>

    <o:p></o:p> 

    Hi, I'm wondering if there is corruption of the aspnetdb.mdf file because of improperly shutting down, Please make sure that the file is still exists under App_Data folder of your web application. If it is, make a copy of it, though it might be corrupt already.

    <o:p></o:p> 

    You can try attach your .mdf file with SQL Server. You can try to attach mdf only by using sp_attach_single_file_db too if there is only one ldf file for the db. 

    <o:p></o:p> 

    If you are using SQL Server instead of SQL Server Express, please back up the last transaction log too.<o:p></o:p>

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;253817<o:p></o:p>

     

    Monday, February 5, 2007 12:14 AM
  • User805391953 posted

    Hi,

    thanks for the answer but I haven't the file "aspnetdb.mdf " in my application directories...In addition,I use Sql Express Edition and I don't think is possible to attach my database file ...

    Monday, February 5, 2007 11:24 AM
  • User805391953 posted

    Hi,

    thanks for the answer but I haven't the file "aspnetdb.mdf " in my application directories...In addition,I use Sql Express Edition and I don't think is possible to attach my database file ...

    Monday, February 5, 2007 11:25 AM
  • User113421904 posted

    Hi,

    thanks for the answer but I haven't the file "aspnetdb.mdf " in my application directories...In addition,I use Sql Express Edition and I don't think is possible to attach my database file ...

    What is your error message?

    The original error of this thread is not able to attach the aspnetdb.mdf file using SQL Express:

    [SqlException (0x80131904): An attempt to attach an auto-named database for file C:\WebSites\Test_TimeTracker\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.]

    So If you are using SQL Express as Membership Provider, there is aspnetdb.mdf file, please use the asp.net Administrative Tool to configure users and roles, this tool will create this file automatically.

    If your error message shows different .mdf file name, please make sure the file is already exists under App_Data folder and it is accessible. Please check your connection string as well, e.g.

    <connectionStrings>
     <add name="aspnet_staterKits_Test_TimeTracker" connectionString="Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
     <remove name="LocalSqlServer"/>
     <add name="LocalSqlServer"connectionString="Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
    </connectionStrings>


     

    Monday, February 5, 2007 8:32 PM
  • User805391953 posted
    Ok,thanks for your answers...I finally copy and paste the same database.mdf from my usb flash and it worked...so a good thing to do anytime is to backup always your application...[:P]
    Wednesday, February 7, 2007 7:18 AM
  • User1797453978 posted

    I need help attaching databases. I tried to use the attach string provided by my host and it doesn't work. I don't know what to do.

    This is my string:

    <add name="CLASSIFIEDSDB" connectionString="Data Source=.\SQLExpress;Integrated Security=True;Database=banana.qsh.es_ff7d004e-b930-40da-8e7a-dab56782dd2f;AttachDBFilename=|DataDirectory|CLASSIFIEDSDB.mdf;User Instance=True" providerName="System.Data.SqlClient"/>   
            <remove name="LocalSqlServer"/>
           
    <add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=SSPI;Database=banana.qsh.es_ca9a7de8-492f-47c5-bbc6-e2ad4eb27a40;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>

    </connectionStrings>

    The host is http://banana.qsh.eu 

     

    Should I keep the ldf files or they are created automatically when I tried to run the page for the first time?

     

    Thursday, May 17, 2007 2:55 PM
  • User1797453978 posted

    If you can help me, please write directly to my email at ajarncarlos@gmail.com

    Thanks 

    Thursday, May 17, 2007 2:56 PM
  • User555306248 posted

    This is not depend upon what version you used...

    See in the App_Data folder or see in Server Explorer in VS, you will get there..

    Friday, May 18, 2007 12:19 AM
  • User-727466326 posted

    After a lot of trials, tribulations and errors, I found a solution to the problem.  Please mark this as an answer if it turns out helpful.  I cannot vouch for the security of this solution.  Some say its bad, but I say its good because it works.

     

    1. Update your connection string to the following: 

    Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

     Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

    2. Under the application pool properties in the identity tab, set "Network Service" or "ASP.NET" as the security account.

    3. Make sure you grant read and write permissions to "Network Service" or "ASP.NET" account.  Give full permissions and adjust later to be on the safe (or unsafe) side.

     

    COMMENTARY: 

    The security model for .NET, ASP.NET, SQL Server, and Windows is extremely difficult to wrap your head around.  You must know each technology’s security model and how they interplay with one another in order to become proficient and productive.  The exception error messages don't really tell you what the problem is.  There are no books or articles that cover these security models comprehensively. Consequently, many developers figure out what works by trial and error and wind up compromising security, which renders the security system useless.

    Saturday, February 9, 2008 9:39 PM
  • User555306248 posted

    Try to set User Instance=false

    Sunday, February 10, 2008 10:35 PM
  • User2087812504 posted

    I've just had this same problem and figured out what the cause is.

    This error message is happening because the account which the Application Pool is running does not have sufficient permissions to auto-attach the database.

    Please note this is NOT the account which is used to run the website.  My Application Pool is the DefaultAppPool and was using Network Service account to run.

    I changed this to Local System and it started working.  However, I believe this has security implications which I cannot be bothered to figure out just yet.  I'm just happy it's working.

    Friday, July 3, 2009 5:30 AM
  • User555306248 posted

     Yes thats right ,but recommondation is to use  Network Service account to run.

    Sunday, July 5, 2009 10:55 PM
  • User1867937566 posted

     THE MAIN PROBLEM IS THIS ERROR:

    "OR IT IS LOCATED ON UNC SHARE",  I REALLY DON´T KNOW WHAT'S THE PROBLEM, BECAUSE I´M NOT SHARING ANYTHING... IF YOU FIND SOMETHING... PLESE WRITE ME aebordones@hotmail.com

    Friday, July 10, 2009 11:28 AM
  • User555306248 posted

     CAn you put here more error details here, So that it will be helpful to solve it

    Sunday, July 12, 2009 11:02 PM
  • User-1446326904 posted

    Guys, if you are getting this error running it locally, Visual Studio provides the connection string that you can use. Go to Server Explorer-> and aright click on the name of the .mdf file. Click "Modify Connection..." and select "Advanced" at the bottom, there's a Textbox with the connection string. Use this if testing locally. When Deployed, change the "AttachDbFilename" to the deployed location. (i.e. "AppData/DBName.mdf". Hope this helps.

    Friday, October 2, 2009 1:56 PM
  • User555306248 posted

    Another way to solve this query...Thanks

    Monday, October 5, 2009 11:05 PM
  • User1829104532 posted

    It happened to me because the path to the mdf file in the connection string was wrong.

    Thursday, September 23, 2010 10:36 AM
  • User157771612 posted

    how do you change to local system?

    Wednesday, May 18, 2011 5:48 AM
  • User-1406409624 posted

    It took me OVER 5 DAYS to solve this, working for 10-16 hours at a time. Scott Gu SAVED MY LIFE ON THIS ONE!!!!!

    This is YOUR ANSWER...IT'S NOT COMPLICATED! Thank you, Jesus!!!  :-D

    It's all about your ConnectionString. U really should just Change the ConnectionString Name to "LocalSqlServer". But make sure you add <remove name="localsqlserver"/> first.  MS has a build in connection... , you know what, just read the end of Scott Gu's blog. It LITERALLY took me about 2 minutes to fix this in VWD! OMG SOOOOOOOOOOOOOOOOOOOOOOOOO RELIEVED!!!!!!!

    (FYI: I've never contributed to a forum b4, but i felt the need to this time, all of these answers were SOOOOOOOOO frickin COMPLICATED, hard to follow, and worst: the DIDN'T SOLVE MY "LOGIN" PROBLEM! So i HAAAAAAAAAD to share once i found this answer. Good luck, coders!)

    So here you go, I just copied and pasted the most important part RT from his Blog RT to this forum:

    ASP.NET 2.0 ships with a built-in connection string called “LocalSqlServer” which by default is configured to use a SQL Express database, and which by default the Membership, Roles, Personalization, Profile and Health Monitoring services are configured to use.

     

    The easiest way to have your application automatically take advantage of your newly created SQL database is to just replace the connectionstring value of this “LocalSqlServer” setting in your app’s local web.config.

     

    For example, if I created my database on the local machine in an “appservicesdb” database instance and was connecting using Windows Integrated security, I would change my local web.config file to specify this:

     

    <configuration>

     

        <connectionStrings>

            <remove name=”LocalSqlServer”/>

            <add name="LocalSqlServer" connectionString="Data Source=localhost;Initial Catalog=appservicesdb;Integrated Security=True" providerName="System.Data.SqlClient"/>

        </connectionStrings>

     

    </configuration>

    Here's the blog URL:

    http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx

    Friday, June 24, 2011 11:19 AM
  • User265919062 posted

    Your solution fixed my error perfectly. However, the link you listed:

    http://communities.microsoft.com/newsgroups/previewFrame.asp?ICP=sqlserver2005&sLCID=us&sgroupURL=microsoft.private.sqlserver2005.express&sMessageID=%253CuQCkdV5UFHA.2092@CPMSFTNGSA04.privatenews.microsoft.com%253E

    wouldn't pull up for me. Is the page gone, or is the link not right?

    Thanks for helpful post.

    Friday, July 8, 2011 2:06 PM
  • User-220649653 posted

    The main problem is wrong path in app.config file or threw file out from this localization. I had the same problem and first I checked path which was wrong and moreover I checked my Sql Server Path and wasn't file inside!!!

    Sunday, September 18, 2011 5:13 PM