Click here to Skip to main content
12,701,051 members (29,595 online)
Click here to Skip to main content
Articles » Database » Database » Databases » Downloads


24 bookmarked

Custom Configuration SQL Connection String Section

, 23 Jan 2006 CPOL
This solution demonstrates enforcing valid SQL connection strings in the App.Config file.
Fair License

Copyright (c) 2005 Theodore William Bouskill

Usage of the works is permitted provided that this
instrument is retained with the works, so that any entity
that uses the works is notified of this instrument.

using System;

using System.Collections;
using System.Configuration;

using System.Data;
using System.Data.SqlClient;

namespace CodeProject.SqlConnectionCollection
	/// <summary>
	/// Container for Main() function to demonstrate the SqlConnectionConfigurationStrings solution
	/// </summary>
	class MainClass
		static void Main(string[] args)
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

				// The following code generates a boolean that dictates enforcement of a
				// mandatory SqlConnectionConfigurationSection
				object appSetting = ConfigurationSettings.AppSettings["MandatorySqlConnectionConfigurationSection"];
				bool mandatorySqlConnectionConfigurationSection = appSetting == null ? false : Convert.ToBoolean(appSetting);

				using (SqlConnectionConfigurationStrings sqlConnCfgStrs = (SqlConnectionConfigurationStrings)ConfigurationSettings.GetConfig(SqlConnectionConfigurationSectionHandler.SectionName))
					// If no SqlConnectionConfigurationStrings were found and they are required, throw an exception
					if ((sqlConnCfgStrs == null) && mandatorySqlConnectionConfigurationSection)
						throw new SqlConnectionConfigurationException(String.Format("Mandatory Configuration Section[{0}] missing from .Config file.", SqlConnectionConfigurationSectionHandler.SectionName));

					if (sqlConnCfgStrs != null)
						// Iterate through the IDs and retrieve each SqlConnectionString
							ArrayList sqlConnCfgStrIDs = sqlConnCfgStrs.IDs;

							for (int i = 0; i < sqlConnCfgStrIDs.Count; i++)
								Console.WriteLine("\nID[{0}] = [{1}]", (String)sqlConnCfgStrIDs[i], sqlConnCfgStrs[(String)sqlConnCfgStrIDs[i]].ToString());

							Console.WriteLine("\"Invalid ID\" = [{0}]", sqlConnCfgStrs["Invalid.ID"].ToString());
							Console.WriteLine("\n\"Invalid ID\" = [Invalid.ID]");


			catch (SqlConnectionConfigurationException exception)
				Console.WriteLine("Main.SqlConnectionConfigurationException: " + exception.Message);
			catch (Exception exception)
				// If the exception was thrown during XML validation or XSLT transformation we need the InnerException
				Console.WriteLine("Main.Exception: " + exception.InnerException != null ? exception.InnerException.Message : exception.Message);

		public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
			Console.WriteLine("Unhandled Exception: " + ((Exception) args.ExceptionObject).Message);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ted Bouskill
Software Developer (Senior)
Canada Canada
Very experienced Senior Software Developer/Manager with natural leadership and proven project management skills.

Started in Civil Engineering leading the integration of PC's into the Engineering process as a Design/Drafting Technician as well as an in-house Software Developer then transitioned into a full time Software Development career in the early 90's.

Ability to change and adapt has led to diverse experience with a wide array of technology and roles from graphics or web development to designing line of business enterprise applications.

Knowledge Base: Sharepoint, C#, SQL, ASP.NET, C++, CSS, HTML, JavaScript, XML, XSLT

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Ted Bouskill
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid