0

I am making a program and i want for the user to be able to connect it to several Databases(SQL or MySQL) dynamically as he/she uses it. So i need to be able to Create Read Update and Delete connection strings (into the app.config ???) and make those changes persistent.

Till now i am able to do most of these things but they are not persistent.Here is some of my code.

    public static class CnnHelper
    {
      public static string ReadCnn(string name)
      {
        return ConfigurationManager.ConnectionStrings[name].ConnectionString;
      }
      public static void UpdateCnn(string name,string cnn)
      {
        ConfigurationManager.ConnectionStrings[name].ConnectionString = cnn;
      }
      public static void InsertCnn(string name, string connectionstring)
      {
        RemoveReadOnly();
        ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings(name, connectionstring));
        AddReadOnly();
      }
      public static List<string> GetAllCnnNames()
      {
        return ConfigurationManager.ConnectionStrings
                                   .Cast<ConnectionStringSettings>()
                                   .Select(v => v.Name)
                                   .ToList();
      }
      private static void RemoveReadOnly()
      {
        typeof(ConfigurationElementCollection)
                .GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue(ConfigurationManager.ConnectionStrings, false);
        //ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings());
      }
      private static void AddReadOnly()
      {
        typeof(ConfigurationElementCollection)
                .GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue(ConfigurationManager.ConnectionStrings, true);
        //ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings());
      }
    }

Why it doesn't keep the changes ?

How i can do this?

1
  • You usually only need on connection string to connect to each database, you might need CRUD sql statements, is that what you meant? Why are you setting bReadOnly through reflection? Commented Jun 26, 2018 at 12:50

1 Answer 1

1

I think ideally you should keep a user profile to manage the database connection strings for each user instead of letting them directly change the app config.

If you still prefer to use the app settings config file, then you need to use the Configuration.Save method to save the changes in the configuration.

Refer: https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configuration.save?view=netframework-4.7.1

        // Add an entry to appSettings section.
        int appStgCnt =
            ConfigurationManager.AppSettings.Count;
        string newKey = "NewKey" + appStgCnt.ToString();

        string newValue = DateTime.Now.ToLongDateString() +
          " " + DateTime.Now.ToLongTimeString();

        config.AppSettings.Settings.Add(newKey, newValue);
config.Save(ConfigurationSaveMode.Full);
Sign up to request clarification or add additional context in comments.

1 Comment

Yep. It took me a while but i figured it out! This is the right way to do it! Thank you a lot. I checked it as the answer but i cant mark it as useful yet!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.