Can I Add ConnectionStrings to the ConnectionStringCollection at Runtime?

前端 未结 8 1443
心在旅途
心在旅途 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

提交回复
热议问题