locked
System.Data.SqlClient.SqlException (0x80131904): An attempt to attach an auto-named database for file C:\MyData\database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. RRS feed

  • Question

  • I wrote a WPF application with a LocalDBV11.0. In production I have no problem testing my application, storing my data to the database and others action I can do against the database until I packaged the application to be distributed.

    I get an Error message after I launched the newly installed application:

    Error message:

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

    1- I made sure that SQL Server LocalDBv11 gets downloaded and installed if the customer doesn't have it on his machine.
    2- I verified that the database was copied into the packaged application.
    3- I verified that the permission of the database were Read And Write.
    4- I have looked online and I couldn't found a clear answer; it looks like many programmers experienced this problem with LocalDBV11.0

    ConnectionString Code:

    <connectionStrings>
       <add name="MyConnectionString"
          connectionString="Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\databasename.mdf;Integrated Security=True;Connect Timeout=30"
          providerName="System.Data.SqlClient" />
      </connectionStrings>  


    Thank you in advance for your help

    
    

       

    AA

    Saturday, July 4, 2015 1:58 PM

Answers

  • Couple things to look at:

    • Validate that the path exisits
    • Run the program in administrator mode (sometimes UAC gets in the way of things)
    • Validate the user has FULL control permissions on the folder
    • Validate the mdf and ldf are in the folder
    • Manually create the database to see if that works
    • Manually attach the database to see if that works

    Another thing to consider is just building the database on the fly from scripts rather than attempting an attach.

    I hope you found this post helpful! If you did please vote it as helpful on the left. If it answered your question please mark it as the answer below. :)

    Saturday, July 4, 2015 5:50 PM
  • Hi Ailiam,

    Besides other post, in your connection string, please replace the value |DataDirectory|\databasename.mdf in the AttachDbFilename property with the direct path of the mdf file and check if it works properly.

    Also check if there are more than one connection strings having same name in your web/app config file.

     
    Thanks,
    Lydia Zhang


    Lydia Zhang
    TechNet Community Support


    Monday, July 6, 2015 7:50 AM
  •  After reaching and combining all the suggestions that have been offered, I have finally the way to deploy SQL LocalDB.

     1- Before deploying your application with your database, first make sure that the path of the connection string points to the folder C:\Users\userxxx\Appdata\Roaming\DatabaseName.mdf on the customer's computer.

      e.g.:

    private static stringappdataroamingfolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    Your connectionString can look like this:

    "Data Source=(LocalDB)\\v11.0;AttachDbFilename=" + appdataroamingfolder + "\\myDatabase.mdf;" +
    "Integrated Security=True;Connect Timeout=30";

    By doing so, you will not going to worry about having Access Denied while try to save the database on the C:\\ Drive.  

    2- The second step you can refer to How to: Dynamically Create a Database


     

      

    AA

    Thursday, July 16, 2015 5:09 PM

All replies

  • Couple things to look at:

    • Validate that the path exisits
    • Run the program in administrator mode (sometimes UAC gets in the way of things)
    • Validate the user has FULL control permissions on the folder
    • Validate the mdf and ldf are in the folder
    • Manually create the database to see if that works
    • Manually attach the database to see if that works

    Another thing to consider is just building the database on the fly from scripts rather than attempting an attach.

    I hope you found this post helpful! If you did please vote it as helpful on the left. If it answered your question please mark it as the answer below. :)

    Saturday, July 4, 2015 5:50 PM
  • Hi Ailiam,

    Besides other post, in your connection string, please replace the value |DataDirectory|\databasename.mdf in the AttachDbFilename property with the direct path of the mdf file and check if it works properly.

    Also check if there are more than one connection strings having same name in your web/app config file.

     
    Thanks,
    Lydia Zhang


    Lydia Zhang
    TechNet Community Support


    Monday, July 6, 2015 7:50 AM
  • Hello,

    Thank you for your suggestion, I have tried it and it works only locally meaning on the production machine. But the application doesn't work on another machine such as a client computer.

    I still get us the same error message  

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

      

     

    AA

    Tuesday, July 7, 2015 1:04 PM
  •  After reaching and combining all the suggestions that have been offered, I have finally the way to deploy SQL LocalDB.

     1- Before deploying your application with your database, first make sure that the path of the connection string points to the folder C:\Users\userxxx\Appdata\Roaming\DatabaseName.mdf on the customer's computer.

      e.g.:

    private static stringappdataroamingfolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    Your connectionString can look like this:

    "Data Source=(LocalDB)\\v11.0;AttachDbFilename=" + appdataroamingfolder + "\\myDatabase.mdf;" +
    "Integrated Security=True;Connect Timeout=30";

    By doing so, you will not going to worry about having Access Denied while try to save the database on the C:\\ Drive.  

    2- The second step you can refer to How to: Dynamically Create a Database


     

      

    AA

    Thursday, July 16, 2015 5:09 PM