我想在运行时更改 app.setting 连接字符串。我有一些代码可以更改它,但我不能这样做。运行时没有出现错误。但是在执行过程中没有变化。
这是我的部分代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Punch_Uploader.Properties.Settings.testConnectionString"
connectionString="server=localhost;User Id=root;password=test123;database=test"
providerName="MySql.Data.MySqlClient" />
<add name="Punch_Uploader.Properties.Settings.testConnectionString1"
connectionString="server=172.23.2.52;User Id=root;password=test123;database=test"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
和:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Add(
new ConnectionStringSettings("Punch_Uploader.Properties.Settings.testConnectionString2",
String.Format(
"server={0};Port={1}; database={2};User Id={3};password={4};providerName={5}",
"172.23.2.32", "3306", "test", "root", "test123", "MySql.Data.MySqlClient")
)
);
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
MessageBox.Show(ConfigurationManager
.ConnectionStrings["Punch_Uploader.Properties.Settings.testConnectionString2"]
.ConnectionString);
上面的代码对我不起作用....
请帮我解决这个问题。