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

Update to SQL connection string validation code

Chris (who I meet down at SC Code Camp 2006) left a comment on my C# connection string validation code pointing out some flaws in my code.  He suggested playing with System.Data.SqlClient.SqlConnectionStringBuilder

I redid the code to take advantage of this helper class.  Here is the updated version of the code.  Note that I only validate Data Source, Initial Catalog, User ID and Password because that is the only thing our software uses right now.

public static bool IsValidSQLConnectionString(string cn)
{
    bool rv = true;
    try
    {
        SqlConnectionStringBuilder sqlSB = new SqlConnectionStringBuilder();
        sqlSB.ConnectionString = cn;
        if (string.IsNullOrEmpty(sqlSB.DataSource))
        {
            throw new ApplicationException("Data Source missing");
        }
        if (string.IsNullOrEmpty(sqlSB.InitialCatalog))
        {
            throw new ApplicationException("Initial Catalog missing");
        }
        if (string.IsNullOrEmpty(sqlSB.UserID))
        {
            throw new ApplicationException("User ID missing");
        }
        if (string.IsNullOrEmpty(sqlSB.Password))
        {
            throw new ApplicationException("Password missing");
        }
    }
    catch (Exception)
    {
        rv = false;
    }
    return rv; 
}

 

Print | posted on Monday, May 14, 2007 2:59 PM | Filed Under [ .NET ]

Feedback

Gravatar

# re: Update to SQL connection string validation code

Cool, I'm glad that worked out!
5/14/2007 3:43 PM | Shadowin
Gravatar

# re: Update to SQL connection string validation code

Is there a reason for throwing exceptions? It looks like a better approach may be to just return false in the "if" blocks instead of throwing exceptions and catching them. There's added overhead associated with the exceptions, and generally should only be used for "exceptional" situations.

If you were logging them, or bubbling them up, then that would be another matter, but it appears that you're just swallowing them...
5/14/2007 4:17 PM | Dusty
Gravatar

# re: Update to SQL connection string validation code

Dusty,

I have no idea! I'm actually working on a blog post to see if it is more expensive to create an exception than let the code touch a path that will not impact its return value.

I think for clean code purposes, an exception should be thrown. Because, sometime in the future, you will want to bubble that exception or handle a specific exception differently. I also don't know if there is enough of a resource expenditure to warrant not tossing an exception every time.
5/14/2007 4:24 PM | Tim Hibbard
Gravatar

# Links (5/16/2007)

.NET Update to SQL connection string validation code LINQ To Objects - Part 2 Custom Generic EventArgs
5/16/2007 8:55 PM | Member Blogs
Gravatar

# re: Update to SQL connection string validation code

They are asking for validation code but they didnt give me one, so I dont know.
8/6/2007 5:39 AM | Lungile
Gravatar

# re: Update to SQL connection string validation code

A type is passed into the Validation facade's Validate<T> method. This type matches the type of the LINQ object. Like I mentioned before, the Validation Application Block requires that validators (classes that inherit from the base Validator class) be defined either on the business object's property declaration, or in the configuration file of the target application.
3/31/2010 5:17 AM | sito di backgammon
Gravatar

# re: Update to SQL connection string validation code

they can focus on their core business activity, while our team of skilled professionals expertly represent their brand.
casino
3/24/2011 3:07 AM | best online casinos
Gravatar

# re: Update to SQL connection string validation code

Believe in yourself! Have faith in your abilities! Without a humble but reasonable confidence in your own powers you cannot be successful or happy.
artho@
5/18/2011 12:23 PM | Electronic Cigarette Review
Gravatar

# re: Update to SQL connection string validation code

Believe in yourself! Have confidence in your abilities! Without a humble confidence, but reasonable in your own powers you can not be successful happy.

6/8/2011 12:50 AM | SEO reseller
Gravatar

# re: Update to SQL connection string validation code

There's no word to describe such a great masterpiece. You made such an interesting piece to read, giving every subject an enlightenment for us to gain knowledge and information without any arguments to deal with. Thank you very much and more power!
7/5/2011 12:19 PM | irs back taxes
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: