Click here to Skip to main content
15,880,608 members
Articles / Mobile Apps
Article

Simple SQL Connection String Manager Class

Rate me:
Please Sign up or sign in to vote.
2.97/5 (23 votes)
14 Sep 2004CPOL1 min read 143.5K   2K   37   7
Simple class that helps accessing and managing SQL Connection Strings.

Introduction

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

C#
// Code fragments:

/// <SUMMARY>
/// 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".

C#
// 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
try
{
   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.

History

Posted on 15th September 2004.

License

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


Written By
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.

Comments and Discussions

 
GeneralVery useful Pin
mgdekker19-Aug-10 1:06
mgdekker19-Aug-10 1:06 
QuestionWhat is the license obligation associated with this class usage? Pin
Bhavanandham24-Jul-07 11:09
Bhavanandham24-Jul-07 11:09 
AnswerRe: What is the license obligation associated with this class usage? Pin
Pawel Zarzycki29-Jul-07 23:03
Pawel Zarzycki29-Jul-07 23:03 
GeneralRe: What is the license obligation associated with this class usage? Pin
Bhavanandham30-Jul-07 9:43
Bhavanandham30-Jul-07 9:43 
GeneralSqlConnectionStringBuilder Pin
Seth Juarez25-Feb-06 9:45
Seth Juarez25-Feb-06 9:45 
GeneralRe: SqlConnectionStringBuilder Pin
Bhavanandham29-Jan-07 6:39
Bhavanandham29-Jan-07 6:39 
GeneralRe: SqlConnectionStringBuilder Pin
hudhta8-May-07 0:35
hudhta8-May-07 0: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.