none
Flustered... A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    Question

  • Hello everyone,

    I have been playing with a Standard Version of VS 2005 and SSE 2005 and I just cannot get these two to interact together well.  I am sure it is a noob problem but I have seen this error addressed on this forum and I am just not getting it.  Here is exactly what I am doing.

    I want to create a database within SSE using the Management Studio.  Then, I want to connect to it with VS2005.  Both SSE and VS2005 are local.  I just cant seem to get this to work. 

    I always seem to get this error "An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Second.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

    One thing... I create the website with Location: HTTP.  I don't even know anymore why I do this.. I think because I like to be able to pull up a browser and get to my app.  Other than that I am doing nothing special.  However, I get the above error when I hit F5 to run the app inside VS2005.

    Is there someplace where a complete noob can find a thorough and simple covering of this?  Again, I have read through some of the previous posts on this and I don't understand too many of them and it seems as if there are at least 13 causes of which I don't know which one I have.  Help!

    Thanks.

     

    Tuesday, December 05, 2006 5:15 PM

Answers

  • You connection string should look like...

    Data Source=.\SQLEXPRESS;Database=<your database name>;Integrated Security=SSPI

    The error you're getting is because VS generated a User Instance connection string. A User Instance is a separate instance of SQL Express that is started at runtime which tries to attach the database specified for use by the application. Your database is already attached to the parent instance of SQL Express, and databases can only be attached to a single instance at a time. By changing the connection string, you'll be using the parent instance, where the database is already attached, rather than the User Instance.

    Mike

    Sunday, December 10, 2006 10:38 PM
    Moderator

All replies

  • Jens,

    Thanks for the response.  I have not resolved this yet as I don't really understand how to do many of the suggestions but let me ask you a follow-up question. 

    One suggestion was to go into SSE and manually attach to the database and then change the connection string to point to the pre-attached database.

    I am creating the database in SSE prior to attempting to get at it with VS.  Everything I am doing is on my local machine.  What would my connection string need to look like for this scenario?  I don't need a user-instance or auto-attach.

    I have looked at sites like ConnectionStrings.com and see a number of different connection strings but do not know which one corresponds to what I am trying to do.

    Thanks again for any help.

    Thursday, December 07, 2006 5:02 PM
  • You connection string should look like...

    Data Source=.\SQLEXPRESS;Database=<your database name>;Integrated Security=SSPI

    The error you're getting is because VS generated a User Instance connection string. A User Instance is a separate instance of SQL Express that is started at runtime which tries to attach the database specified for use by the application. Your database is already attached to the parent instance of SQL Express, and databases can only be attached to a single instance at a time. By changing the connection string, you'll be using the parent instance, where the database is already attached, rather than the User Instance.

    Mike

    Sunday, December 10, 2006 10:38 PM
    Moderator
  • Hi Mike

     

    Thanks a lot for a connection string Trip. After suffering of SQL Server 2005 mistries and watching different Web Log my problem with your connection string trip finaly be solved. So here i given my previous and new connection string as an example for others who still have the same problem.

    Previous connection String in Web.Config File:

                               

    <connectionStrings>

    <add name="NORTHWNDConnectionString" connectionString="Data  Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

    </connectionStrings>

     

     

    New Connection String in Web Config File:

     

    <connectionStrings>

    <add name="NORTHWNDConnectionString" connectionString="Data Source=<SQL Server 2005 Name>;Database=NORTHWND.MDF;

    Integrated Security=SSPI; " providerName="System.Data.SqlClient"/>

    </connectionStrings>

     

    Happy Programming!

    G.M.Lakho

     

     

     

     

     

     

    Wednesday, May 09, 2007 7:05 PM
  •  

    I, too have a problem along these lines.

     

    The original 'UNC Share' problem was gotten around by pre-attaching the ASPNETDB.MDF file through the Studio System.

     

    I then incorpoated the roleManager excerpt into my web.config file and having played around with the ConnectionStringName for a while, I am now getting errors claiming the following:

     

    Parser Error Message: The connection name 'Data Source=IT2001\SQLEXPRESS;Initial Catalog=ASPNETDB;Integrated Security=True' was not found in the applications configuration or the connection string is empty.

    This stanza is in exactly the same form as the other connection string for the other database file I am using, so I am trying to work out what is happening.

     

    None of the documentation I have on the system even mentions this sort of problem.

     

     

    Thursday, August 30, 2007 3:51 PM
  •  

    I had the same problem, if you give a unique value for the database name problem will get solved, you can rearrange the connection string as follows

     

    Database=UNIQUEHASHVALUE;Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|dbname.mdf
     
     
    It worked for me!
     
     
    Saturday, December 15, 2007 2:47 PM
  • Hi Abdul,

     

    Just a warning, you're idea of combining a unique value for the database name along with a User Instance is actually likely to cause you problems down the road. SQL Server associates the name of the database with the file path where the database files are located. If the value returned by the |DataDirectory| macro ever changes, you will generate an error because your UNIQUEHASHVALUE is associated with the old file path, so you will get an error indicating that the database name already exists in a different location.

     

    When using User Instances, you should really avoid setting the Database= parameter. (Same for the Initial Catalog, which is just another way to establish a database name.)

     

    I know that this is working right now, but the database path location can change unexpectedly because of things that VS does under the covers. I just want to make sure that you and others are aware of this behavior so you're not surprised down the road if you start getting errors. And so you know what is causing them.

     

    Regards,

    Mike

    Saturday, December 15, 2007 7:11 PM
    Moderator
  • Hi Anthony
    I don't know if your problem has been solved. If not, I would take a good look at the name of the file in the error message because if that file doesn't exist in your computer you will have to create in norder to open it.

    Phil
    Saturday, October 25, 2008 2:01 AM
  • For what it's worth, I just encountered this exact problem while using VS 2008 Pro SP1 and SQL Server 2008 Developer Edition. This is not as technical-sounding and there is no code involved, but here is what I did. I shut down VS 2008 and SQL Server and then opened them back up and used the Add Connection wizard. Worked perfectly. The definition of madness is to attempt the same thing and expect different results. I guess madness pays off sometimes.
    Sunday, January 04, 2009 10:01 AM
  • What happened with me is I was running a sample application that was using the Entity Data Model and a SQLEXPRESS database, with a Test Project also, in Release Candidate ASP.NET MVC.   When I tried to run the tests I got an error because the Test project's App.config didn't have the database connection string in it.   After I copied the connection string in from the main ASP.NET MVC project's Web.config to the Test project's App.config and tried to run the test, I then got the "A database with the same name exists, or specified file cannot be opened" error.   I tried all the different fixes I found on forums to get rid of this but none of them worked.  Then I went to the Server explorer and connected to the database from there and examined the ConnectionString property.  It was essentially the same as what I had with the one difference that instead of using |DataDirectory| it was specifying the exact fully-qualified path to the .mdf file.  I copied this ConnectionString into the needed spot in the full connection string (between &quot; and &quot; as shown below) and the error went away and the Test passed.

    <add name="MyConnectionStringName" connectionString="metadata=res://*/Models.MyDataModel.csdl|res://*/Models.MyDataModel.ssdl|res://*/Models.MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MySampleApp\MySampleApp\App_Data\MySampleAppDB.mdf;Integrated Security=True;User Instance=True&quot;" providerName="System.Data.EntityClient"/>

    • Proposed as answer by TommyRojo Sunday, October 02, 2011 7:30 PM
    Saturday, February 21, 2009 9:23 AM
  • Hi All,

    I too experience the same problem. I just tried to create an entity class to use in my application using Linq.

    I got this error

    An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\MYE.mdf failed. A database with the same name \
    exists, or specified file cannot be opened, or it is located on UNC share


    when I executed the folllowing command from VS 2008 cmd prompt.

    C:\Users\sekar\Documents\Visual Studio 2008\Projects\MYEx\MYEx>sqlmetal /namespa
    ce:MYE /code:Database.cs /pluralize /functions /sprocs /views "C:\Program Files\
    Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\MYE.mdf"

    Pls help me to create an entity class.
    Monday, April 06, 2009 4:45 AM
  • Is there any way to change it back, so it doesn't use the actual sql server express on my local machine, and uses the mdf file in the project directly?

    Thanks
    AG
    Friday, April 17, 2009 2:45 AM
  • Hello Anthony... it´s July 10th, 2009 and I don´t found the problem... but I think it´s gotta be with "or is located on UNC Share" error...   Greetings from Panamá!!!
    Friday, July 10, 2009 2:50 PM
  • And if i am not using Visual Studio and i am writing my web application with a NotaPad, but i have installed the unique really free downloadable application from microsoft SQLServer Express Edition 2005 with management?

    Friday, July 10, 2009 3:34 PM
  • Is there any way to change it back, so it doesn't use the actual sql server express on my local machine, and uses the mdf file in the project directly?

    Thanks
    AG
    Right click on your App_Code folder in your solution explorer and click "Add existing Item". Then locate the mdf file you want to use from your directory. That will fix your problem
    Thursday, August 20, 2009 1:36 PM
  • THE SOLUTION IS HERE

    The solution is simple... just right click the App_Data folder in Windows Explorer, go to the Security tab, click the "Advanced" button, click "Change permissions", select the option to propagate changes to children objects and click "Apply".
    • Proposed as answer by Babak_bsn Monday, July 26, 2010 5:22 AM
    Sunday, November 15, 2009 1:54 AM
  • I too had the same problem i tried many options but it didnt work for me after some permutations and combinations I solved the problem myself by specifying the connection string like this string connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Examples\MVC\EmployeePortal\EmployeePortal\App_Data\EmployeeDB.mdf;Integrated Security=True;User Instance=True"; Before the connection string used to be .. connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|EmployeeDB.mdf;User Instance=true" The difference is in this attribute.. AttachDBFilename From where did u get the "connString" value ? I went to the server explorer in the VS environment there i selcted the corresponding database => Properties=> Connection string... Hope this helps.. Thanks.. Aditya Marla
    Friday, December 11, 2009 6:35 PM
  • You connection string should look like...

    Data Source=.\SQLEXPRESS;Database=<your database name>;Integrated Security=SSPI

    The error you're getting is because VS generated a User Instance connection string. A User Instance is a separate instance of SQL Express that is started at runtime which tries to attach the database specified for use by the application. Your database is already attached to the parent instance of SQL Express, and databases can only be attached to a single instance at a time. By changing the connection string, you'll be using the parent instance, where the database is already attached, rather than the User Instance.

    Mike


    This answer worked for me too... tnx
    Wednesday, January 06, 2010 11:07 AM
  • I'm also using Linq and  in my case it was very simple. I had project in svn repository and I basically checked it out to the wrong folder. 
    So the actual path in connection string in app.config wasn't corresponding to the real location of the mdf file. 
    Hope that helps!


    Monday, February 22, 2010 7:22 PM
  • Samsong,

    In my case, your solution doesnt work.

    I still get the same error:

    An attempt to attach an auto-named database for file C:\Documents and Settings\Paul\WroxBook\WroxUnited\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.

    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:\Documents and Settings\Paul\WroxBook\WroxUnited\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 made changes to the web.config as you suggested:

    <

     

    connectionStrings>

    <

     

    add name="WroxUnited" connectionString="Data Source=.\SQLEXPRESS;Database=WroxUnited.mdf;Integrated Security=SSPI"

     

     

    providerName="System.Data.SqlClient" />

    <

     

    add name="WroxUnited2" connectionString="Data Source=.\SQLEXPRESS;Database=ASPNETDB.mdf;Integrated Security=SSPI"

     

     

    providerName="System.Data.SqlClient" />

    </

     

    connectionStrings>

     

    What am I missing here? Please help??

    Tuesday, April 20, 2010 4:07 AM
  • Once you attach database in to SQL server in SQL server studio management express, it removed permission to your current user, System user, Authenticated User.

    Solution:

    1. Right click on .mdf file and select properties

    2. Select securities tab.

    3. Check is their these three more user are there, if not add then and grant full permissions These users are Authenticated user, SYSTEM user and your Username.

    4. Repeat, same for .log file.

    Thats all. You are good to Go.

    • Proposed as answer by l3ny Monday, May 10, 2010 7:16 PM
    Saturday, May 08, 2010 3:48 PM
  • On server explorer window(VS) Right Click on the database and close connection, And you won't have the following problem:

    An attempt to attach an auto-named database for file C:\Documents and Settings\Paul\WroxBook\WroxUnited\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.

     

    ~leny



    l3ny
    • Proposed as answer by l3ny Monday, May 10, 2010 7:21 PM
    Monday, May 10, 2010 7:19 PM
  • Yes it was!  Thanks
    Monday, December 06, 2010 6:34 PM
  • In my case the clue was "specified file cannot be opened"

    so, just copy it by hand on |DataDirectory|\NorthWind.mdf and away you go!

    regards

    Oruc

    Tuesday, March 29, 2011 11:03 PM
  • Thanks man, that worked out really well for me. I was doing the same thing. Copying the web.config to the test project's App.config.

    Cheers!

    Tom

    Sunday, October 02, 2011 7:27 PM
  • Hello everyone,

    I have been playing with a Standard Version of VS 2005 and SSE 2005 and I just cannot get these two to interact together well.  I am sure it is a noob problem but I have seen this error addressed on this forum and I am just not getting it.  Here is exactly what I am doing.

    I want to create a database within SSE using the Management Studio.  Then, I want to connect to it with VS2005.  Both SSE and VS2005 are local.  I just cant seem to get this to work. 

    I always seem to get this error "An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Second.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

    One thing... I create the website with Location: HTTP.  I don't even know anymore why I do this.. I think because I like to be able to pull up a browser and get to my app.  Other than that I am doing nothing special.  However, I get the above error when I hit F5 to run the app inside VS2005.

    Is there someplace where a complete noob can find a thorough and simple covering of this?  Again, I have read through some of the previous posts on this and I don't understand too many of them and it seems as if there are at least 13 causes of which I don't know which one I have.  Help!

    Thanks.

    I had the same issue using LINQ, VS 2012 Express, and SQL Express at the same location. My solution was to 'detach' the database. For some reason LINQ does not like it when SQL is using the database. Once detached you can run your queries.

    I'm trying to find another solution because this option is not ideal although this is the only solution that is presented in the MSDN LINQ walkthough.


    If you're developing something, you want someone like me to break it and to let you know about it...

    Sunday, March 03, 2013 8:56 PM
  • My connection string is <add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Database=appdb.mdf;Integrated Security=SSPI"/>

    and the error is 

    Cannot open database "appdb.mdf" requested by the login. The login failed.
    Login failed for user 'MCA-PC\MCA'.

    Saturday, November 16, 2013 5:21 PM
  • It does no good to respond to a very old and answered thread with another "me too" response (and no useful information).  Please start your own thread and provide the appropriate information about your system (as suggested in the sticky posts at the top of the forum).  Before you do that, read all of the responses in this thread slowly and carefully.  Your connection string is not correct - examples have already been posted.  Make the effort to compare yours to the others and find the differences.
    • Edited by scott_morris-ga Monday, November 18, 2013 2:52 PM correct grammar
    Monday, November 18, 2013 2:51 PM