Click here to Skip to main content
Click here to Skip to main content

Simple SQL Connection String Manager Class

, 14 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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

// 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)

Share

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.

Comments and Discussions

 
GeneralVery useful Pinmembermgdekker19-Aug-10 1:06 
QuestionWhat is the license obligation associated with this class usage? PinmemberBhavanandham24-Jul-07 11:09 
AnswerRe: What is the license obligation associated with this class usage? PinmemberPawel Zarzycki29-Jul-07 23:03 
GeneralRe: What is the license obligation associated with this class usage? PinmemberBhavanandham30-Jul-07 9:43 
Thanks!
GeneralSqlConnectionStringBuilder PinmemberSeth Juarez25-Feb-06 9:45 
GeneralRe: SqlConnectionStringBuilder PinmemberBhavanandham29-Jan-07 6:39 
GeneralRe: SqlConnectionStringBuilder Pinmemberhudhta8-May-07 0:35 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 14 Sep 2004
Article Copyright 2004 by Pawel Zarzycki
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid