Tim Hibbard

CEO for EnGraph software
posts - 628 , comments - 1634 , trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

C# Code to validate SQL Connection Strings


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: , , ,

Print | posted on Thursday, May 10, 2007 7:56 AM | Filed Under [ .NET ]

Feedback

Gravatar

# re: C# Code to validate SQL Connection Strings

You can also declaratively secure connection strings using [SqlClientPermission], see

http://thoughtpad.net/alan-dean/sqlclientpermission.html
5/10/2007 9:44 AM | Alan Dean
Gravatar

# re: C# Code to validate SQL Connection Strings

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.
5/10/2007 11:00 AM | Shadowin
Gravatar

# re: C# Code to validate SQL Connection Strings

Seria mejor si no tuviera un try-catch
10/15/2007 9:50 AM | Didier
Gravatar

# re: C# Code to validate SQL Connection Strings

It only check the Connection String Parameters
but nothing about valid/invalid parameters
1/27/2009 2:24 PM | تبلیغات
Gravatar

# re: C# Code to validate SQL Connection Strings

Thx for sharing this function
2/13/2009 9:34 AM | David
Gravatar

# re: C# Code to validate SQL Connection Strings

Interesting function.
I loved it
Thanks for the post.
4/30/2011 12:50 PM | طراحی وب سایت
Gravatar

# re: C# Code to validate SQL Connection Strings

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.
1/5/2012 6:06 PM | ch
Gravatar

# re: C# Code to validate SQL Connection Strings

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

3/20/2012 1:03 PM | sharad vishwakarma
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: