1

I have a Windows Forms app with an integrated LocalDB SQL database through Visual Studio with a RecipeDB.mdf file that resides in the folder with the rest of the application. I am also using LINQ to SQL. I have everything working fine, and I am ready to publish the app and turn in the project for school.

The one problem I have is that for some reason if I use AttachDbFilename=|DataDirectory|\RecipeDatabase.mdf in my connection string in my App.Config, it doesn’t save to the database. It requires that I use the full path of C:\Users\Tania\source\repos\Old\HealthierRecipes\HealthierRecipes\RecipeDB.mdf.

That has been fine while I was creating the project, but it won’t work for publishing it. I need the program to be able to be installed wherever people want it, and for it to be able to find its database.

So how do I get it to save to the correct place? Is there a way to make a variable for the App.config connection string and have people put their path in a text file? Or is there some way to get the database to acknowledge the |DataDirectory| path?

Any help would be appreciated.

TBaildon
  • 33
  • 1
  • 5
  • See following : https://www.xspdf.com/resolution/52760824.html – jdweng Feb 20 '21 at 22:43
  • There are ways to save extra files in ASP.Web in that, and I can possibly translate that to Winforms, but that doesn't solve the issue of my database not saving information if it doesn't have the full path in the App.Config, or how to have a user put their own path in the App.Config if I can't find another way. I am still somewhat new to programming, so any help is appreciated. – TBaildon Feb 20 '21 at 22:59
  • The default folder is the same as the location of the exe c# file. – jdweng Feb 21 '21 at 00:51
  • Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Feb 21 '21 at 02:10
  • Charlieface - In this case, no, because the database is supposed to be local and attached to the application. But thank you. – TBaildon Feb 21 '21 at 03:14

1 Answers1

0

I was able to find the answer here, however, I want to give some explanation.

publish a project with local database

Within my project while I am working on it, I still have to have the full path written in the connection string to work. But just before I go to publish it, I change it to |DataDirectory|, then publish, and the program works great once it is published that way. I have no idea why, but it works, and that is good enough for now.

TBaildon
  • 33
  • 1
  • 5