Can I Add ConnectionStrings to the ConnectionStringCollection at Runtime?

前端 未结 8 1423
心在旅途
心在旅途 2020-12-15 18:38

Is there a way where I can add a connection string to the ConnectionStringCollection returned by the ConfigurationManager at runtime in an Asp.Net application?

I hav

相关标签:
8条回答
  • 2020-12-15 19:12
    var cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@"/");
    cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(params));
    
    cfg.Save();
    

    Be Advised this will cause your website to recycle since it modifies the config file. Check out http://msdn.microsoft.com/en-us/library/4c2kcht0(VS.80).aspx

    0 讨论(0)
  • 2020-12-15 19:15

    No, you can't modify the config file at runtime, it isn't intended for that. Maybe you could use the Enterprise Libraries Configuration to do that.

    0 讨论(0)
提交回复
热议问题