views:

77

answers:

4

I want my app.config (or my web.config for that matter) to read from an SQL database instead of an XML file. How can I do that?

A: 

Your application will need to know how to connect to the database, so your config file will, at least, need a relevant connection string. However, once you have that in place, you can use ADO.NET to connect to the database and select whatever further config you desire.

AdamRalph
+3  A: 

You can't. The .NET configuration system is only file based. Anyway, how would your program know how to connect to the database ? Of course, you can put some settings in the database, but you program will still need a config file to get the DB connection string (unless you hard-code it, which is of course not recommended)

Thomas Levesque
To get get your settings, you would need your connection string... but to get your connection string, you would need your settings... but to get your settings you would need your connection string..... AHHHH... if we keep going we could end up with an answer like this!! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
Zoidberg
+1  A: 

Three words: why, oh why?

App config files are there for a reason. Introducing a database as a dependency for settings required for the app to even run, is not a good idea - if it even could be done.

Wim Hollebrandse
That's overly general. We do this a lot in our systems because of the sheer number -- it's much easier to (re)configure dozens or hundreds of servers centrally vs. having to edit all of those individual configuration files. Plus, if you wrap the configuration calls you can do things like refresh the config values from the DB, something not easily done with the app.config files.
Joe
I doubt storing essential config settings for a wide range of apps in a central DB is a good strategy. Furthermore, app configs are far more easily versioned than DB data.
Wim Hollebrandse
A: 

I can't see any way you can. You can easily replace the concept of app.config settings with fields stored in a custom database - you just need to write your own configuration class that reads the values from the DB and then makes them accessible as static properties or as Dictionary or similar.

However, web.config is different - asp.net expects to read it's configuration values from a file called web.config. If you really, really want to store values meant for web.config in a database then the best you could do was write an app that would generate the XML file and write it out. But I would really question why on earth you would want to?

Dan Diplo