簡體   English   中英

不更改app.config

[英]Not Change app.config

我想在運行時在app.config中更改連接字符串

我編寫的代碼如下:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["test"].ConnectionString = "sadasd";
//config.ConnectionStrings.SectionInformation.ForceSave = true;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

但是app.config不會改變!

您可以嘗試使用我使用過的這種方法,該方法已在WinForms和MySQL項目中為我工作:

 private void EditConString(string connName, string user, string pwd, string server,string database)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings[connName].ConnectionString = "Server=" + server + ";User Id=" + user + ";password=" + pwd + ";database=" + database + ";ConnectionTimeout = 60;Allow Zero Datetime=True";
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
        }

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM