0

I've got a problem with a project with Visual Studio 2017.

I want to save data in a local SQL Server database but when I run the script this error always happens at connection.open();:

System.Data.SqlClient.SqlException: "An attempt to attach an auto-named database for file C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

Here's the code for the connection:

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf;Integrated Security=True;Connect Timeout=30");
connection.Open();

Do you guys know why that error comes and how to solve it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JBM
  • 1
  • Possible duplicate of [An attempt to attach an auto-named database for file ....database1.mdf failed](https://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed) – SBFrancies Jan 09 '18 at 21:21
  • 2
    As answered previously: your `AttachDbFileName` has an erroneous **space** between the `C:` and the `\Users` - remove that and you're done! – marc_s Jan 09 '18 at 21:22
  • I'm just trying to connect to the Database – JBM Jan 09 '18 at 21:22
  • 1
    `@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf;Integrated Security=True;Connect Timeout=30"` this should be your connection string to complete @marc_s 's comment – interesting-name-here Jan 09 '18 at 21:29
  • Duplicate of: https://stackoverflow.com/questions/48173516/i-receive-an-attempt-to-attach-an-auto-named-database-for-file – marc_s Jan 09 '18 at 21:30
  • 1
    Possible duplicate of [I receive An attempt to attach an auto-named database for file](https://stackoverflow.com/questions/48173516/i-receive-an-attempt-to-attach-an-auto-named-database-for-file) – interesting-name-here Jan 09 '18 at 21:32

0 Answers0