最近在做一个项目,用到了Enterprise Library,其中用户登陆以后,修改数据库链接,代码如下:
 
           Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationSection section = config.ConnectionStrings;

            config.ConnectionStrings.ConnectionStrings[0].ConnectionString = connectionString;

            config.Save(ConfigurationSaveMode.Full);

其中connectionString是新的链接字符串,保存修改后,再次链接数据库,却发现并没有改变。为了测试问题出在甚么地方,我添加了一个检测congfig文件修改的事件,代码如下:

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationSection section = config.ConnectionStrings;

            fileWatcher = new ConfigurationChangeFileWatcher(config.FilePath, section.SectionInformation.Name);
            fileWatcher.ConfigurationChanged += new ConfigurationChangedEventHandler(fileWatcher_ConfigurationChanged);

            fileWatcher.StartWatching();


        void fileWatcher_ConfigurationChanged(object sender, ConfigurationChangedEventArgs e)
        {
            richTextBox.AppendText(Environment.NewLine + "数据库连接地址已改变!");
        }

实际测试中发现,点击保存后,可能过了5,6秒的样子,congfig文件才发生改变,我想这可能是缓存的问题,但是怎么让她立即更新呢,请大家多多帮忙。

 

相关文章:

  • 2021-08-06
  • 2021-10-25
  • 2022-01-29
  • 2022-12-23
  • 2022-01-02
  • 2021-06-28
  • 2021-06-03
  • 2022-01-26
猜你喜欢
  • 2021-05-18
  • 2021-09-24
  • 2022-01-19
  • 2022-12-23
  • 2022-01-31
  • 2021-11-02
相关资源
相似解决方案