0

我需要覆盖所使用的 app.config 中的 DefaultDatabase 值DatabaseFactory.CreateDatabase() of DAAB

我有下面的代码,它表明更改成功,但是当我稍后在代码中引用默认数据库时,它不再具有下面代码保存的更改。无论是在调试模式下还是作为已编译的 exe,都会发生这种情况。

Database db = DatabaseFactory.CreateDatabase();

更改 app.config 的代码

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var dbSettings = (DatabaseSettings)ConfigurationManager.GetSection("dataConfiguration");

        string connectionString = ConfigurationManager.ConnectionStrings[dbSettings.DefaultDatabase].ConnectionString;

        connectionString = connectionString.Replace("DATABASETOREPLACE", dstuff.Database).Replace("SERVERTOREPLACE", dstuff.Server);

        config.ConnectionStrings.ConnectionStrings[dbSettings.DefaultDatabase].ConnectionString = connectionString;

        config.Save(ConfigurationSaveMode.Modified, true);
        ConfigurationManager.RefreshSection("connectionStrings");

app.config 值



4

1 回答 1

0

尝试ConfigurationManager.RefreshSection("configuration"); 代替ConfigurationManager.RefreshSection("connectionStrings");

于 2012-03-29T15:39:52.697 回答