none
I try to add a database file to my project and am told it exists already RRS feed

  • Question

  • I copied a SQL SERVER MDF file to my app_data folder in my Visual Studio project and tried to connect, but got this error when the code opens the database connection.

    System.Data.SqlClient.SqlException
      HResult=0x80131904
      Message=An attempt to attach an auto-named database for file 
    C:\gideonwroteprograms2\EchoTheory\EchoTheory\bin\Debug\App_Data\EchoDatabase.mdf
    failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    A possible clue:

    A few weeks ago I tried to connect create the same database from within the server explorer window, and then run a script to populate its tables.   That did work, but since then I had to reinstall visual studio, and the program supposedly no longer finds that database, but when I put an MDF version in the app_data folder, maybe its finding somehow that old version and raising an error about duplication?   Or is there another problem?

    Thanks

      

    Tuesday, December 3, 2019 6:51 PM

Answers

  • Hi Gidmaestro,

    Welcome to MSDN forum.

    Thanks for the reply of Key Tucker.

    >> I copied a SQL SERVER MDF file to my app_data folder in my Visual Studio project and tried to connect, but got this error when the code opens the database connection.

    ## According to your description, I have something to confirm with you. What’s your project type and your Visual Studio edition? Could you share me how you connect to your MDF database file? You could refer to the link to connect to your MDF database file.

    If possible, you could share us your source project so that we can try our best to check it on our side.

    In addition, based on your error message, my best guess is the file path in your code does not contain the database file. So please check whether there is an existed file in your file path(app_data). Or l recommend that you can change the file path by:

    AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

    For more information and suggestion, please have a look at the similar issue.

    Hope all above will help you and any feedback will be expected.

    Best Regards,

    Anna 


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Wednesday, December 4, 2019 8:11 AM
  • Hold on:

    I tried the connection string I got from 'properties', and it worked.

    I failed before because I was trying to make the connection string work in 'production', that is, when I distribute the program to users, the program has to find the database.

    So I had the following code:

    returnString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EchoDatabase.mdf;Integrated Security=True;Connect Timeout=30"
    returnString = returnString.Replace("|DataDirectory|", newDir)
    • Marked as answer by Gidmaestro Thursday, December 5, 2019 12:19 PM
    Wednesday, December 4, 2019 12:02 PM

All replies

  • Try deleting the bin folder and rebuilt the app.  Maybe the mdf is left in the bin folder but not in the solution
    Wednesday, December 4, 2019 12:59 AM
  • Hi Gidmaestro,

    Welcome to MSDN forum.

    Thanks for the reply of Key Tucker.

    >> I copied a SQL SERVER MDF file to my app_data folder in my Visual Studio project and tried to connect, but got this error when the code opens the database connection.

    ## According to your description, I have something to confirm with you. What’s your project type and your Visual Studio edition? Could you share me how you connect to your MDF database file? You could refer to the link to connect to your MDF database file.

    If possible, you could share us your source project so that we can try our best to check it on our side.

    In addition, based on your error message, my best guess is the file path in your code does not contain the database file. So please check whether there is an existed file in your file path(app_data). Or l recommend that you can change the file path by:

    AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

    For more information and suggestion, please have a look at the similar issue.

    Hope all above will help you and any feedback will be expected.

    Best Regards,

    Anna 


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Wednesday, December 4, 2019 8:11 AM
  • Hi Anna

    The project is a windows form project, written in visual basic, and using Visual Studio Community Edition (the latest).

    I added the database by going to server-explorer, and clicking on 'Add connection', and choosing SQL Server MDF file.

    I notice there is a red 'x' mark in server explorer over the MDF file icon for the database.  The X goes away if I do a REFRESH.

    When I right-click on the connection, I get:

    Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\gideonwroteprograms2\EchoTheory\EchoTheory\App_Data\EchoDatabase.mdf;Integrated Security=True;Connect Timeout=30

    In contrast, when I click on what my code produced, I get:

    Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\gideonwroteprograms2\EchoTheory\EchoTheory\bin\Debug\App_Data\EchoDatabase.mdf;Integrated Security=True;Connect Timeout=30

    The provider is: .NET Framework Data Provider for SQL Server 

    I notice it (the provider) does not say 'localdb'.

    I don't know how to upload the entire project for you to look at.


    • Edited by Gidmaestro Wednesday, December 4, 2019 11:56 AM
    Wednesday, December 4, 2019 11:49 AM
  • Hold on:

    I tried the connection string I got from 'properties', and it worked.

    I failed before because I was trying to make the connection string work in 'production', that is, when I distribute the program to users, the program has to find the database.

    So I had the following code:

    returnString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EchoDatabase.mdf;Integrated Security=True;Connect Timeout=30"
    returnString = returnString.Replace("|DataDirectory|", newDir)
    • Marked as answer by Gidmaestro Thursday, December 5, 2019 12:19 PM
    Wednesday, December 4, 2019 12:02 PM
  • Hi Gidmaestro,

    Thanks for your sharing and we are so glad to hear that your issue is solved.

    If the issue is solved, Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    If you have any other Visual Studio General Questions issue in the future, please feel free to let us know.

    Thanks you for helping us build a better Visual Studio!

    Have a nice day.

    Best Regards,

    Anna


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, December 5, 2019 9:24 AM