Click here to Skip to main content
13,353,322 members (53,558 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 14 Sep 2004

Simple SQL Connection String Manager Class

, 14 Sep 2004
Rate this:
Please Sign up or sign in to vote.
Simple class that helps accessing and managing SQL Connection Strings.


When you are programming managed code that needs to access some SQL database, you need to provide a connection string. In many cases, you have to build it manually, store somewhere, and allow user to change some of the components of this connection string. To help you especially with the latter task, I provide you this (very simple, indeed) C# class.

This class accepts all valid SQL connection string value names and its content (for currently used values). You can review them at MSDN.

Using the code

// Code fragments:

/// Manages Sql Connection String
/// </SUMMARY>
public class SqlConnectionString : ICloneable
    public string ConnectionString {get;set;}

    public string Server       {get;set;}
    public string Database     {get;set;}
    public string UserID       {get;set;}
    public string Password     {get;set;}
    public bool Authentication {get;set;}

SqlConnectionString class has six public properties:

  • ConnectionString - sets or gets whole connection string. When there is no database nor server specified, returned connection string is empty.
  • Server - The name or network address of the instance of SQL Server to which to connect.
  • Database - The name of the database.
  • UserID - The SQL Server login account. Valid when not using Windows Integrated Security.
  • Password - The password for the SQL Server account logging on. Valid when not using Windows Integrated Security.
  • Authentication - When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

You can set any valid SQL connection string to ConnectionString property. When you then read this property, you will get always the same value names. I.e., you can set "Trusted_Connection=yes" or "Trusted_Connection=true" or "Integrated Security=true", but you will always get "Integrated Security=SSPI".

// initialize SqlConnectionString 
SqlConnectionString connStr = new SqlConnectionString();
connStr.ConnectionString = 
    "Trusted_Connection=yes;Addr=MyServer;Initial Catalog=MyDatabase";

// manage SqlConnectionString, i.e. some UI wizard

// ...
connStr.Server = txtServer.Text;
connStr.Database = txtDatabase.Text;
// ...

// try to create connection
   using (SqlConnection connection = 
            new SqlConnection (connStr.ConnectionString))
     // execute some query
     // ...

This class also implements ICloneable, so you can clone your connection string, and modify its copy separated from the cloned object.


Posted on 15th September 2004.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pawel Zarzycki
Product Manager Cognitum
Poland Poland
MBA and M.A. in Management has several years of strong experience in managing international projects within research and development area as well as projects financed from public sources. He has earned his MBA diploma from Oxford Brookes University, M.A. in Management from Polish Open University and B.Sc. in Computer Science from Warsaw University of Technology.

You may also be interested in...


Comments and Discussions

GeneralVery useful Pin
mgdekker19-Aug-10 2:06
membermgdekker19-Aug-10 2:06 
QuestionWhat is the license obligation associated with this class usage? Pin
Bhavanandham24-Jul-07 12:09
memberBhavanandham24-Jul-07 12:09 
AnswerRe: What is the license obligation associated with this class usage? Pin
Pawel Zarzycki30-Jul-07 0:03
memberPawel Zarzycki30-Jul-07 0:03 
I've attached no specific licence for this code sample. It is provided "as is" with no guarantee Wink | ;) Use it as you like wherever you wish, also within commercial products.


-= Pawel Zarzycki =-

GeneralRe: What is the license obligation associated with this class usage? Pin
Bhavanandham30-Jul-07 10:43
memberBhavanandham30-Jul-07 10:43 
GeneralSqlConnectionStringBuilder Pin
Seth Juarez25-Feb-06 10:45
memberSeth Juarez25-Feb-06 10:45 
GeneralRe: SqlConnectionStringBuilder Pin
Bhavanandham29-Jan-07 7:39
memberBhavanandham29-Jan-07 7:39 
GeneralRe: SqlConnectionStringBuilder Pin
hudhta8-May-07 1:35
memberhudhta8-May-07 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 14 Sep 2004
Article Copyright 2004 by Pawel Zarzycki
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid