-2

I've made connection string in app.config and used it whenever I needed to make a SQL connection using this code:

static string connectionString = ConfigurationManager.ConnectionStrings["name"].ConnectionString;

It works perfectly if I use full path to the database but I want to make it universal (so I could install my application on any other PC) and I used |DataDirectory| like this:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Biblioteka.mdf;Integrated Security=True"

After using |DataDirectory|, I can access the database (I'm not getting any errors while debugging) through my app but I cannot update it, delete records from tables etc...

Is there a way I can keep using app.config for connection string and making it universal at the same time?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [How do I update |DataDirectory|](https://stackoverflow.com/questions/12266924/) – Dour High Arch May 30 '20 at 16:49
  • 1
    Does this answer your question? [Why saving changes to a database fails?](https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails) – SMor May 30 '20 at 17:17

1 Answers1

0

|DataDirectory| can resolve to many different things depending on your application and how it is installed, but typically it is a read-only folder shared among all users which you should not change. Moreover, repairing or uninstalling your app will typically overwrite or erase |DataDirectory|, so even if you could update it your updates would soon be deleted.

|DataDirectory| is a good location for data which must not change, or for a source or seed database for your applications. If you want users to update data, you should copy the data from |DataDirectory| to the user data folder, that location is writeable and does not get overwritten or deleted, then make all user changes to the copy.

For more information read How do I update |DataDirectory|.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90