N-Tier Database Settings and Management for ASP.NET
Ok, so the purpose for this post is because, I myself, as well as countless others have been searching for this answer all over the net. I provide it here as a reference and resource.
Here is the situation:
- You build an ASP.NET Web Application which is part of a Visual Studio Solution. You store your Database connection strings in your Web.Config file under the correct <connectionStrings /> section. However, in order for a true N-Tier solution in Web Applications you also have a BLL, DAL, and possibly other projects that contain your Classes and Logic for the UI layer of the Web Application. So the problem is this: how do I share my ASP.NET project's Connection String information with the DAL and other layers?
This is a simple enough idea and makes perfect logical sense, however the answer is not always easy to find. I finally sent Scott Gu and Scott Mitchell, kudos to them, the question to get the official "Microsoft" answer. As Scott explained, any Class libraries are allowed to have, as part of the project the standard "App.Config" file, basically the Windows Forms version of the "Web.Config" us ASP.NET people are used to. The App.Config file is structured a little differently than the normal Web.Config file, however it still allows for a connectionStrings setting. Attached to this post you will find a ZIP file with the sample code I use in this post.
So how does it all work?
Simple Enough:
- Ok first we either need to create a project or open an existing solution to work with. If you are reading this I assume we have mastered basic Visual Studio tasks
- The example code attached will have an ASP.NET Web Application called "WebApplication1" as well as a Class Library called "DAL". Most of the DAL work I do is all done with Datasets and TableAdapters utilizing the Visual Studio IDE and GUI, so I normally remove the default "Class1.vb" file that Visual Studio generates.
- Ok so first we need to make sure our Web.Config file has all of the correct information in the <connectionStrings /> section. Here is a sample:
|
<connectionStrings> <clear/> <add name="SampleConn" connectionString="Data Source=MyDBServer;Initial Catalog=Sample;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> |
- So now the task is to setup the equivalent in our DAL class library. To do this we need to "Add" the Application Configuration File. The default name is "app.config". Just like our web.config file it is pre-populated with settings. You can leave them, they are ok.
- Now the fun part: we need to add the connection string parameter to the app.config section so that our TableAdapters have something to look at. But wait! How does this work, how will the TableAdapters know to look at the web.config and not the app.config you ask? Magic, seriously… when you finish with your solution and build the applications, the Classes are compiled into Classes, as long as you get the naming right, which we will describe below, then the "app.config" file for an ASP.NET Web Application is the "web.config" file. So as long as the name of the connection string matches then the Class Libraries could care less about whether it is named web.config or app.config.
- So here is the "app.config" code:
|
<connectionStrings> <clear/> <add name="DAL.My.MySettings.SampleConn" connectionString="Data Source=MyDBServer;Initial Catalog=Sample;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> |
- Notice that the "name" for the connection string is actually the physical namespace for the app.config file. This is required! Make sure that you specify the name based off of the Namespace specified in your project settings for the Class Library. If your root namespace was Samples.DAL then the name should be "Samples.DAL.My.MySettings.SampleConn"
- Ok, that's it. Build your project and make a change to your web.config file. Notice the data is no longer connected? This means everything worked!
- Good Luck!