Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software


Update - I made the code better here

Here is a code snippet I use to validate SQL connection strings:

 

/// <summary>
/// Checks a connection string for validity
/// </summary>
/// <param name="cn">SQL Connection string to validate</param>
/// <returns>True if connection is valid.</returns>
/// <history>
/// [Tim Hibbard] 12/03/2006 Created
/// </history>
public static bool IsValidSQLConnectionString(string cn)
{
bool RV = true;
try
{
if (!cn.Contains("Data Source"))
{
throw new MissingMemberException("Missing Data Source");
}
if (!cn.Contains("Initial Catalog"))
{
throw new MissingMemberException("Missing Initial Catalog");
}
if (!cn.Contains("User ID"))
{
throw new MissingMemberException("Missing User ID");
}
if (!cn.Contains("Password"))
{
throw new MissingMemberException("Missing Password");
}
string[] contents = cn.Split(char.Parse(";"));
if (!contents[0].StartsWith("Data Source"))
{
throw new MissingMemberException("Missing Data Source");
}
if (!contents[1].StartsWith("Initial Catalog"))
{
throw new MissingMemberException("Missing Initial Catalog");
}
if (!contents[2].StartsWith("User ID"))
{
throw new MissingMemberException("Missing User ID");
}
if (!contents[3].StartsWith("Password"))
{
throw new MissingMemberException("Missing Password");
}
}
catch (Exception)
{
RV = false;
}
return RV;
}
 
Technorati tags: , , ,
Posted on Thursday, May 10, 2007 7:56 AM .NET | Back to top


Comments on this post: C# Code to validate SQL Connection Strings

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
You can also declaratively secure connection strings using [SqlClientPermission], see

http://thoughtpad.net/alan-dean/sqlclientpermission.html
Left by Alan Dean on May 10, 2007 9:44 AM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
This code forces connection strings to be in a specific order and forces optional elements to the end. Also, I believe that those keys are case-insensitive.

I recommend using SqlConnectionStringBuilder in the System.Data.SqlClient namespace.
Left by Shadowin on May 10, 2007 11:00 AM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
Seria mejor si no tuviera un try-catch
Left by Didier on Oct 15, 2007 9:50 AM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
It only check the Connection String Parameters
but nothing about valid/invalid parameters
Left by تبلیغات on Jan 27, 2009 2:24 PM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
Thx for sharing this function
Left by David on Feb 13, 2009 9:34 AM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
Interesting function.
I loved it
Thanks for the post.
Left by طراحی وب سایت on Apr 30, 2011 12:50 PM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
El try catch es necesario si accede a distintas posiciones dentro de su array sin saber si siquiera existen.
Quizás convenga validar eso primero.
Left by ch on Jan 05, 2012 6:06 PM

# re: C# Code to validate SQL Connection Strings
Requesting Gravatar...
This code is not workig properly in my application and it is continue returing false value that's why i'm able to understand logic so plz give some better logic than this

Left by sharad vishwakarma on Mar 20, 2012 1:03 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net