0

Is it possible to write in the <connectionStrings> node in app.config and web.config using C#?

I need to add

<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" />
J.P Masangcay
  • 759
  • 2
  • 10
  • 28
  • Yes, it's possible to put a connection string in a config file. What's preventing you from doing that? – David Dec 20 '15 at 12:26
  • @David Let me update my question. – J.P Masangcay Dec 20 '15 at 12:31
  • @David How can I write in using C#? – J.P Masangcay Dec 20 '15 at 12:33
  • I think you can use the `ConfigurationManager` to write updated settings and save them back to the current config file. Conversely, you can edit any XML file in .NET code pretty easily. It's not really clear why you're looking to do this, so it's not entirely obvious what your best option would be. Can you elaborate on what problem you're trying to solve? – David Dec 20 '15 at 12:36
  • @David I have a Web server that will be adding databases on certain events. I need to create that connectionstring along with the database when created using C#. – J.P Masangcay Dec 20 '15 at 13:01
  • 1
    Possible duplicate of [How do you modify the web.config appSettings at runtime?](http://stackoverflow.com/questions/719928/how-do-you-modify-the-web-config-appsettings-at-runtime) – weir Dec 20 '15 at 13:03
  • That seems like a bad idea. If a database is indeed a dynamic entity in your web application (which it probably shouldn't be, but I suppose there are exceptions to that) then the connections to those databases should also be dynamic and data-driven. The application should have its own transactional database, which itself should be storing information about dynamically created entities. In this case, that would be identifiers for and information about the other databases, including connection strings. – David Dec 20 '15 at 13:04
  • Possible duplicate: http://stackoverflow.com/questions/360024/how-do-i-set-a-connection-string-config-programatically-in-net – Triet Doan Dec 20 '15 at 13:04
  • @David Its a bit complicated. But In my web server, I will be passing data to multiple databases (and it will be adding up). I am using entity framework DF and I have modified the context so I could route or use any connection string from the app.config – J.P Masangcay Dec 20 '15 at 13:12
  • @J.PMasangcay: It still seems like storing dynamic information in a database is going to be better than storing it in a config file. Config files are really meant for environmental configuration, not runtime-created objects. Event if for no other reason than writing to a file in a multi-threaded environment is replete with problems. – David Dec 20 '15 at 13:16
  • @David Can you provide any links on what I can do instead? Because, right now I have all my databases with their own connection string in my app/web config. – J.P Masangcay Dec 20 '15 at 14:21

1 Answers1

0
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");
Guy
  • 46,488
  • 10
  • 44
  • 88