您可以在运行时更改ConnectionString配置值吗?
技术问答
197 人阅读
|
0 人回复
|
2023-09-12
|
运行时能否更改?app.config中的ConnectionString值?根据MSDN文档,应该可以使用ConnectionString属性获取或设置连接字符串。
0 X% O& J; T8 f4 B2 b) o5 c我的代码如下所示:
) a# v5 E) P3 f3 n" \* bConnectionStringSettings mainConnection = ConfigurationManager.ConnectionStrings["mainConnection"];mainConnection.ConnectionString = "Data Source=SERVER;Initial Catalog=" NewDatabaseName ";Integrated Security=True";我收到的错误是:未处理的异常:System.Configuration.ConfigurationErrorsException:配置为只读。: H4 G: H1 k, d8 S3 y8 `5 @0 D
" d. C: h2 N) | 解决方案:
: u0 Z% @. q& M Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text; myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text; myConfiguration.Save();参考:http : //www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-4 W8 i9 ?0 i. E2 w
Time.aspx |
|
|
|
|
|