Wednesday, January 31, 2007

Sql Server 2005 - using SqlConnectionStringBuilder

Problem:
Method to connect to sql server 2005


Solution:

class attributes and accessor functions

private SqlConnection dbConn = null;


private string m_username = string.Empty;
private string m_password = string.Empty;
private string m_catalog = string.Empty;
private string m_datasource = string.Empty;
private string m_application = string.Empty;
private string m_version = string.Empty;
private bool m_IntegratedSecurity = false;

///
/// Name of user who has the appropriate connection privilges
///

public string User_Name
{
get { return (m_username); }
set { m_username = value; }
}

///
/// Password of user trying to connect
///

public string User_Password
{
get { return (m_password); }
set { m_password = value; }
}

///
/// Name of the catalog inside of SqlServer
///

public string DB_Catalog
{
get { return (m_catalog); }
set { m_catalog = value; }
}

///
/// Name of Server where SqlServer is installed
/// can use either the Name of the server or
/// the IP address
///

public string DB_Server
{
get { return (m_datasource); }
set { m_datasource = value; }
}

///
/// Name of application connecting to SqlServer
///

public string Application
{
get { return (m_application); }
set { m_application = value; }
}

public string DB_Version
{
get { return (m_version); }
set { m_version = value; }
}

public bool bool_IntegratedSecurity
{
get { return (m_IntegratedSecurity); }
set { m_IntegratedSecurity = value; }
}



public bool Connect()
{


//build new SqlConnectionStringBuilder object
System.Data.SqlClient.SqlConnectionStringBuilder csb =
new SqlConnectionStringBuilder();

try
{
//use Integrated Security
csb.IntegratedSecurity = this.bool_IntegratedSecurity;

if (this.DB_Server.Length > 0)
{
csb.DataSource = this.DB_Server;
}
else
{
this.int_error_level = ErrorLevel.Warning;
this.str_last_message = "Connection Error: You must provide the DataSource";
return (false);
}

if (this.Application.Length > 0)
{
csb.ApplicationName = this.Application;
}
else
{
csb.ApplicationName = "cRESqlServer";
}

if (this.DB_Catalog.Length > 0)
{
csb.InitialCatalog = this.DB_Catalog;
}
else
{
csb.InitialCatalog = "master";
}

csb.ConnectTimeout = 4;


this.dbConn = new SqlConnection();
this.dbConn.ConnectionString = csb.ConnectionString;
this.dbConn.Open();

this.DB_Version = this.dbConn.ServerVersion;


//success
return (true);
}
catch (System.Data.SqlClient.SqlException ex)
{
this.int_error_level = ErrorLevel.SevereError;
this.str_last_message = "Connect Error (SqlException):" + ex.Message.ToString();
this.HandleError();
return (false);
}
catch (System.Exception ex)
{
this.int_error_level = ErrorLevel.SevereError;
this.str_last_message = "Connect Error (Exception):" + ex.Message.ToString();
this.HandleError();
return (false);
}

}


#region ERROR LEVEL and LAST MESSAGE

private string mLastMessage = string.Empty;
private ErrorLevel mErrorLevel = 0;

public string str_last_message
{
get { return (mLastMessage); }
set { mLastMessage = value; }
}
public ErrorLevel int_error_level
{
get { return (mErrorLevel); }
set { mErrorLevel = value; }
}

#endregion

No comments: