Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

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; 
}

 

Posted on Monday, May 14, 2007 2:59 PM .NET | Back to top


Comments on this post: Update to SQL connection string validation code

# re: Update to SQL connection string validation code
Requesting Gravatar...
Cool, I'm glad that worked out!
Left by Shadowin on May 14, 2007 3:43 PM

# re: Update to SQL connection string validation code
Requesting Gravatar...
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...
Left by Dusty on May 14, 2007 4:17 PM

# re: Update to SQL connection string validation code
Requesting Gravatar...
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.
Left by Tim Hibbard on May 14, 2007 4:24 PM

# Links (5/16/2007)
Requesting Gravatar...
.NET Update to SQL connection string validation code LINQ To Objects - Part 2 Custom Generic EventArgs
Left by Member Blogs on May 16, 2007 8:55 PM

# re: Update to SQL connection string validation code
Requesting Gravatar...
They are asking for validation code but they didnt give me one, so I dont know.
Left by Lungile on Aug 06, 2007 5:39 AM

# re: Update to SQL connection string validation code
Requesting Gravatar...
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.
Left by sito di backgammon on Mar 31, 2010 5:17 AM

# re: Update to SQL connection string validation code
Requesting Gravatar...
they can focus on their core business activity, while our team of skilled professionals expertly represent their brand.
casino
Left by best online casinos on Mar 24, 2011 3:07 AM

# re: Update to SQL connection string validation code
Requesting Gravatar...
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@
Left by Electronic Cigarette Review on May 18, 2011 12:23 PM

# re: Update to SQL connection string validation code
Requesting Gravatar...
Believe in yourself! Have confidence in your abilities! Without a humble confidence, but reasonable in your own powers you can not be successful happy.

Left by SEO reseller on Jun 08, 2011 12:50 AM

# re: Update to SQL connection string validation code
Requesting Gravatar...
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!
Left by irs back taxes on Jul 05, 2011 12:19 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net