Click here to Skip to main content
Licence 
First Posted 16 May 2005
Views 32,706
Bookmarked 29 times

SQL ConnectionInfo Class

By | 16 May 2005 | Article
A class for building SQL connection strings from specified properties. Class can also parse SQLConnection.ConnectionString() into properties using the parse method. Code is fully commented and handles all SQLConnection.ConnectionString properties.
 
Part of The SQL Zone sponsored by
See Also

Sample Image - demobuilder.gif

Sample Image2 - demoparser.gif

Introduction

I wrote this class to help developers with building and updating SQL Connection strings. The ConnectionInfo class can parse and build strings through properties and methods of the ConnectionInfo class. The ConnectionInfo class has two methods. The first method ConnectionInfo.Parse(string), parses SQL Connection strings into ConnectionInfo() properties.

i.e

//Parsing Code-----------------------------------
ConnectionInfo Info = new ConnectionInfo();
Info.Parse("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
MessageBox.Show(Info.DataSource);
//-----------------------------------------------

The second method ConnectionInfo.ToString(), builds a string out of all the available ConnectionInfo() properties.

i.e

//Building Code----------------------------------
ConnectionInfo Info = new ConnectionInfo();
Info.DataSource = "localhost";
Info.InitialCatalog = "Northwind";
MessageBox.Show(Info.ToString());
//-----------------------------------------------

Terms & Conditions

THIS CODE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

References

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlConnectionClassTopic.asp

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

VectorX

Software Developer (Senior)
Codevendor
United States United States

Member

Please visit my personal website http://www.codevendor.com for my latest codes and updates.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionany update ?? Pinmemberalhambra-eidos0:04 25 Sep '08  
QuestionWhy not just create a UDL file? PinmemberChristopher Scholten18:40 17 Jul '05  
GeneralA small bug and some suggestions Pinsussdesynch14:53 9 Jul '05  
Firstly thank you for bringing this class together, it has been very useful to me.
 
I have found out a minor bug. The class can parse the connection string in a wrong way because of the culture settings. For example, I saw that the class interprets the values of the properties "Integrated Security" and "Persist Security Info" as NONE with the following connection string on my machine:
 
Server=(local);Database=Bordro;Integrated Security=sspi;Persist Security Info=false;
 
I realized this occured because these two properties contain the letter "i" whose upper case is "İ" and not "I" in turkish culture which is the default culture on my machine. String.ToUpper function used in the parsing method of the class, is culture dependent and the property "Persist Security Info" is converted as "PERSİST SECURİTY INFO", thus its value can't be evaluated.
 
As connection string properties should be in english only, I thought it's a good idea to force using the culture "en-US" with ToUpper functions independent from the current culture of the application/system:
 
public void Parse(string src)
{
System.Globalization.CultureInfo enCulture = new System.Globalization.CultureInfo("en-US", false);
 
...
 
switch(BreakDownProperties[0].ToUpper(enCulture).Trim())
 
...
}

 
And some suggestions...
 
In my case, I wanted to use only one instance of the class on multiple connection strings so I think it would be good to have a public reset method to get rid of the old values of the properties when needed:
 
public void Reset()
{
//Reset properties
Declare();
}

 
Also, Parse method should rather reset the properties before parsing new connection strings.
 
public void Parse(string src)
{
//Reset properties
Declare();
 
...
}

 
Thanks.

GeneralRe: A small bug and some suggestions Pinmemberalhambra-eidos1:07 25 Sep '08  
QuestionWhat is the standard connection ? PinmemberTrance Junkie23:00 31 May '05  
AnswerRe: What is the standard connection ? PinmemberVectorX6:54 1 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 16 May 2005
Article Copyright 2005 by VectorX
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid