tags:

views:

79

answers:

1

In our web project, we have a custom Database Connection class that creates the SqlConnection object and get the DB connection string for us from the web.config.

While this is nice, there's one problem. All projects inside our web project depend on that web.config being there so that this class can work.

There are needs to use this db connection from our DL in other solutions or projects that should not require a web.config such as a Console project that is trying to use lets say some of the DL methods from our web project to do some data manipulation.

Consequently, when I add some of our BL project methods to my console solution, it's looking to create the DB connection but I have no web.config in my Console application of course so it bombs out.

Is there a better way to manage the creation of the SqlConnection than: a) putting that class in a web project b) making that connection reliant on web.config keys

so that non web based projects can use the BL without that BL referencing and being reliant on a connection that is reliant on ultimately keys in a web.config?

+1  A: 

In your console app those DB connection settings need to go into App.Config, however you clearly don't want to have them stored in both App.Config and Web.Config, so you can use the following technique.

1) Move the DN configuration settings to a single DBSettings.config file

2) Reference that config from Web.Config and App.Config as required

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings file="DBSettings.config">
   ...
</configuration>

This technique is detailed here.

MrTelly
Yea, I can see having a DBSettings.config is going to be good in the long run. But for now, in my console app...which has nothing to do with the web, I can just create an App.Config and the existing code will know to pick it up there instead of web.config automatically?
My console app is in an entire new solution (called Data Loads) and uses about 3 classes that our web project is using (BL, etc.) for getting at our data, but is not using any of the web project in the Data Load solution as it has nothing to do with the web and is just a utility solution
So if I add an App.config file to my Data Load solution or to my console app, then it's going to work just as if I had another xml config file? In other words the call to System.Configuration.ConfigurationManager.AppSettings should work if I added an App.config to my console app?
thanks, added the app.config, works great. Now I see when you need to use that...never needed to before.