我有一個app.config文件,它具有用於我的數據庫的連接字符串。 我想要做的是連接到這就是爲什麼我使用這個代碼不同的數據庫:在運行時更改連接字符串MySql C#
connectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings[nameofConnectionString].ConnectionString = connectionString;
config.Save();
ConfigurationManager.RefreshSection(nameofConnectionString);
它工作得很好,它改變了連接字符串,但問題是,它向我發送一個錯誤說「facerec6 .0.cdcol不存在」
我的默認初始目錄= facerec6.0
我會做什麼?使用RefreshSection
當
<connectionStrings> <- this is the section
<add name="facerec6.0"/> <- this is the element
<add ... />
</connectionStrings>
需要刷新的部分,而不是元素:
如果這是答案,請不要忘記標記爲已回答。謝謝 –