1

I have a C# Winforms application using SQLite as a database. Since SQLite is serverless, there's an actual database file.

I have the following app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="Default" connectionString="Data Source = ./Data/MyDb.db;Version=3;foreign keys=true" providerName="System.Data.SQLite.Core"/>
        <add name="ConstraintsOff" connectionString="Data Source = ./Data/MyDb.db;Version=3;" providerName="System.Data.SQLite.Core"/>
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

One of the functionalities of my app is to allow the user to backup their database, so naturally I also need a functionality that will let them mount that backup in the event they need to.

I know how to create a file selector in UI and to fetch a file from there, but I don't know how to programmatically change the value of connectionString and persist the changes on the file.

I have read this question but the issue seems a bit different there, since it doesn't look like these people are modifying the app.config file but some other configuration file.

I have

ConfigurationManager.ConnectionStrings["Default"].ConnectionString = "Data Source = " + newDbPath + ";Version=3;foreign keys=true";
ConfigurationManager.ConnectionStrings["ConstraintsOff"].ConnectionString = "Data Source = " + newDbPath + ";Version=3";

But these changes will not get saved into the file, so the user will always have to manually mount the DB every time they open it.

As seen in the code, app.config has an entry which in turn has 2 entries added to it, I don't know how to "go down" 2 levels, so I can only reach the value for <connectionStrings>

I'd also like to know if its possible to store a date in app.config, check it and update it programatically as well. I am going to implement a backup functionality that will run every 30 days, and i dont wanna have to set up a windows service so storing a "last date of backup" works for me too.

Dasph
  • 420
  • 2
  • 15
  • There is a `SQLiteConnectionStringBuilder` Can't find docs about it but I think that the docs from [SqlConnectionStringBuilder](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder) should be very similar – Steve Nov 05 '21 at 20:22
  • A connection string is just that, a string. You don't need to save one in the config file, all you need is a string. If you want, you can create your own on the fly. If you want to store the current string (assuming you don't have any secrets in it), you can save it anywhere you want. When I write WinForms apps (mostly for personal use), I have some code I copy in that saves/restores user preferences in an XML file in _Isolated Storage_. Often that includes connection strings (I also have a small component that I use that keeps the last N connection string and puts them in a dropdown – Flydog57 Nov 05 '21 at 20:23
  • @Fltdog57 I'm sorry, perhaps I'm not explaining myself right. What I need is to save the changes to the connection string in app.config, because otherwise the user will have to manually select the database to use every time they open the app. If user is using the OG database and they create a backup for it, I'd like to let them *permanently* select that as the default database when the app runs. – Dasph Nov 05 '21 at 20:34
  • I know how to build a connection string, my problem is permanently storing the newly selected database file as the connection string. – Dasph Nov 05 '21 at 20:34
  • 1
    Sorry, what I'm saying is don't store it in the config file, store it somewhere else. Either in one of the Environment.SpecialFolder folders (ApplicationData if it's user specific, or CommonApplicationData if it's for all users) or in Isolated Storage. It's a string, it's easy to store somewhere. It doesn't need to be in the config file – Flydog57 Nov 05 '21 at 20:49
  • Just implement your own `DbContextFactory` which choose the correct connection string, which you could use like this `using var db = DbFactory.Create(ContextType.BackUp)` and inject the factory and use the db like you normally would – TheGeneral Nov 05 '21 at 21:04
  • I managed to solve it by using this question https://stackoverflow.com/questions/3665869/modify-app-config-permanently-c-sharp – Dasph Nov 05 '21 at 21:46

0 Answers0