Click here to Skip to main content
11,638,881 members (68,599 online)
Click here to Skip to main content
Articles » Database » Database » Databases » Downloads
Add your own
alternative version

Custom Configuration SQL Connection String Section

, 23 Jan 2006 CPOL 79K 888 24
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.

DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
*/
using System;

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

namespace CodeProject
{
	/// <summary>Container class for static SQL Tools</summary>
	public class SqlTools
	{
		/// <summary>Private CTOR because all methods are static</summary>
		private SqlTools() {}

		/// <summary>
		/// This method uses a dummy SQL statement to validate a SqlConnection String
		/// </summary>
		/// <param name="inSqlConnectionString">Required</param>
		/// <returns>true if the SqlConnection String is valid</returns>
		public static bool IsValidSqlConnectionString(SqlConnectionString inSqlConnectionString)
		{
			bool bValidSqlConnectionString = false;

			try
			{
				using (SqlConnection sqlConnection = new SqlConnection(inSqlConnectionString.ToString()))
				{
					sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);

					// Method #1
					/*using (SqlCommand sqlCommand = new SqlCommand("SELECT * FROM dbo.udfThisFunctionIsFake()", sqlConnection))
					{
						sqlCommand.Connection.Open();
						sqlCommand.CommandType = CommandType.Text;
						sqlCommand.ExecuteNonQuery();
					}*/
					// Method #2
					using (SqlCommand sqlCommand = new SqlCommand("dbo.spThisFunctionIsFake", sqlConnection))
					{
						sqlCommand.Connection.Open();
						sqlCommand.CommandType = CommandType.StoredProcedure;
						sqlCommand.ExecuteNonQuery();
					}
				}

				bValidSqlConnectionString = true;
			}
			catch (SqlException exception)
			{
				// exception Numbers 208 & 2812 mean the SqlConnectionString was valid, but the dummy T-SQL failed
				bValidSqlConnectionString = ((exception.Number == 208) || (exception.Number == 2812));
			}
			catch
			{
				bValidSqlConnectionString = false;
			}

			return bValidSqlConnectionString;
		}

		protected static void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
		{
			foreach (SqlError err in args.Errors)
				Console.WriteLine(
						"The {0} has received a severity {1}, state {2} error number {3}\non line {4} of procedure {5} on server {6}:\n{7}",
						err.Source, err.Class, err.State, err.Number, err.LineNumber, err.Procedure, err.Server, err.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.

License

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

Share

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
Web03 | 2.8.150728.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Ted Bouskill
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid