2/21/11

connection class in sql

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using SDMClasslibrary.DA.Master;

namespace SDMClasslibrary.CONN
{
public class Connection
{

public SqlConnection SD_Conn;
public SqlTransaction SD_Trans;

//string ConnectionString;
public Connection()
{

}

private string Connection_String
{
get
{
return ConfigurationSettings.AppSettings["sdmsConnectionString"];
}
}

public SqlConnection Get_Connection()
{
try
{
SD_Conn = new SqlConnection(Connection_String);
SD_Conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return SD_Conn;
}


public void Close_Connection()
{
try
{
if (SD_Conn != null)
SD_Conn.Close();
}
catch (Exception ex)
{
throw ex;
}
}

public void BeginTransaction()
{
try
{
SD_Conn = Get_Connection();
SD_Trans = SD_Conn.BeginTransaction();
}
catch (Exception ex)
{
throw ex;
}
}

public void RollBackTransaction()
{
try
{
SD_Trans.Rollback();
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}

public void CommitTransaction()
{
try
{
SD_Trans.Commit();
Close_Connection();
}
catch (Exception ex)
{
RollBackTransaction();
throw new Exception(ex.Message, ex);
}
}

public Boolean Sql_ExecuteQuery(string strQuery, ref DataSet ds, string str_table)
{
try
{
SqlDataAdapter adpt_cate = new SqlDataAdapter(strQuery, Get_Connection());
adpt_cate.Fill(ds, str_table);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return true;
}

public Boolean Sql_ExecuteQuery(string strQuery)
{
try
{
SqlCommand command = new SqlCommand(strQuery, Get_Connection());
command.ExecuteNonQuery();
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
return true;
}

public Boolean Sql_ExecuteQuery(SqlCommand command)
{
try
{
command.ExecuteNonQuery();
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
return true;
}

public Boolean Sql_ExecuteQueryTransaction(SqlCommand command)
{
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
return true;
}

public void Sql_ExecuteQuery(string strQuery, ref DataSet ds)
{
try
{
SqlDataAdapter adpt_cate = new SqlDataAdapter(strQuery, Get_Connection());
adpt_cate.Fill(ds);
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
}

public string Sql_ExecuteScalarTransaction(string strQuery, SqlConnection Con, SqlTransaction Trans)
{
string return_val = "";
try
{
SqlCommand command = new SqlCommand(strQuery, Con, Trans);
return_val = command.ExecuteScalar().ToString();

return return_val == "" ? "0" : return_val;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}

}

public void Sql_ExecuteQuery(SqlCommand command, ref DataSet ds)
{
try
{
command.CommandTimeout = 1200;
SqlDataAdapter adpt_cate = new SqlDataAdapter(command);
adpt_cate.Fill(ds);
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
}

public Boolean Sql_ExecuteQuery(SqlCommand command, ref DataSet ds, string str_table)
{
try
{
SqlDataAdapter adpt_cate = new SqlDataAdapter(command);
adpt_cate.Fill(ds, str_table);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return true;
}

public void Sql_ExecuteQuery(string strQuery, ref DataTable dtt)
{
try
{
SqlDataAdapter adpt_cate = new SqlDataAdapter(strQuery, Get_Connection());
adpt_cate.Fill(dtt);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}

public Boolean Sql_ExecuteQueryTransaction(string strQuery)
{
try
{
SqlCommand command = new SqlCommand(strQuery, SD_Conn, SD_Trans);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
return true;
}

public string Sql_ExecuteScalar(string strQuery)
{
string return_val = "";
try
{
SqlCommand command = new SqlCommand(strQuery, Get_Connection());
return_val = command.ExecuteScalar().ToString();
Close_Connection();
return return_val == "" ? "0" : return_val;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}

}

public string Sql_ExecuteScalar(SqlCommand command)
{
string return_val = "";
try
{
if (command.ExecuteScalar() == null)
return_val = "";
else
return_val = command.ExecuteScalar().ToString();
Close_Connection();
return return_val == "" ? "0" : return_val;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}

}


///
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
///

///
///
///
///

}
}

No comments:

Post a Comment