简体   繁体   English

C#winform应用程序在运行时运行CRUD ConnectionStrings

[英]CRUD ConnectionStrings at runtime on C# winform app

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. 我正在制作一个程序,我希望用户能够在使用该程序时将其动态连接到几个数据库(SQL或MySQL)。 So i need to be able to Create Read Update and Delete connection strings (into the app.config ???) and make those changes persistent. 因此,我需要能够创建读取更新和删除连接字符串(进入app.config ???),并使这些更改持久化。

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? 我该怎么做?

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. 如果仍然喜欢使用应用程序设置配置文件,则需要使用Configuration.Save方法将更改保存在配置中。

Refer: https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configuration.save?view=netframework-4.7.1 请参阅: https : //docs.microsoft.com/zh-cn/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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM