如何将连接字符串添加到app.config或web.config

本文关键字:config app web 添加 连接 字符串 | 更新日期: 2023-09-27 18:27:24

是否可以使用C#在app.config和web.config中的<connectionStrings>节点中进行写入?

我需要添加

<add name="Lorem" connectionString="metadata=res://*/Models.HOGC.csdl|res://*/Models.HOGC.ssdl|res://*/Models.HOGC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=Ipsum;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

如何将连接字符串添加到app.config或web.config

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("Lorem", "metadata=res://*/Models.HOGC.csdl|res://*/Models.HOGC.ssdl|res://*/Models.HOGC.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=Ipsum;user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");