This is base class witch handle common function in Data Access Layer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
public class DataHelper
{
private string Conncton_string = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
private SqlConnection sqlConn = new SqlConnection();
public static SqlParameter[] _dataParameters = null;
public DataHelper()
{
}
private SqlConnection openConnection()
{
sqlConn.ConnectionString = Conncton_string;
sqlConn.Open();
return sqlConn;
}
private void CloseConnection()
{
sqlConn.Close();
}
//For a execute non query
public void ExecuteNonQuery(CommandType commandType, string commandText, SqlParameter[] parameterCollection)
{
SqlCommand sqlCmd = new SqlCommand();
try
{
sqlCmd.Connection = openConnection();
sqlCmd.CommandType = commandType;
sqlCmd.CommandText = commandText;
sqlCmd.Parameters.AddRange(parameterCollection);
sqlCmd.ExecuteNonQuery();
sqlCmd.Parameters.Clear();
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
sqlCmd.Dispose();
}
}
//to get DataSet
public DataSet GetData(CommandType commandType, string commandText, SqlParameter[] parameterCollection)
{
SqlDataAdapter sqlAdp = new SqlDataAdapter();
DataSet dsItem = new DataSet();
SqlCommand sqlCmd = new SqlCommand();
try
{
sqlCmd.Connection = openConnection();
sqlCmd.CommandType = commandType;
sqlCmd.CommandText = commandText;
if(parameterCollection !=null)
sqlCmd.Parameters.AddRange(parameterCollection);
sqlAdp.SelectCommand = sqlCmd;
sqlAdp.Fill(dsItem);
return dsItem;
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
sqlCmd.Dispose();
}
}
public int ExecuteScaler(CommandType commandType, string commandText, SqlParameter[] parameterCollection)
{
SqlCommand sqlCmd = new SqlCommand();
try
{
sqlCmd.Connection = openConnection();
sqlCmd.CommandType = commandType;
sqlCmd.CommandText = commandText;
sqlCmd.Parameters.AddRange(parameterCollection);
return Convert.ToInt32(sqlCmd.ExecuteScalar().ToString());
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection();
sqlCmd.Dispose();
}
}
}
}
///this is the class derived from above class and execute Stored procedures
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
public class CustomerData:DataHelper
{
public void Save(Entity.Customer cus)
{
try
{
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Cus_name", cus.Cus_Name), new SqlParameter("@Cus_address", cus.Cus_Address), new SqlParameter("@Cus_no", cus.Cus_No)};
ExecuteNonQuery(CommandType.StoredProcedure, "INSERT_Customer", para);
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(Entity.Customer cus)
{
try
{
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Cus_name", cus.Cus_Name), new SqlParameter("@Cus_address", cus.Cus_Address), new SqlParameter("@Cus_no", cus.Cus_No) };
ExecuteNonQuery(CommandType.StoredProcedure, "UPDATE_Customer", para);
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
}
public DataSet SelectCustomerList()
{
try
{
return GetData(CommandType.StoredProcedure, "Select_Customers", null);
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
}
public Customer SelectCustomer(string id)
{
try
{
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Cus_No", id) };
DataSet dsCus= GetData(CommandType.StoredProcedure, "Select_Customer", para);
Customer cus = new Customer();
cus.Cus_No = dsCus.Tables[0].Rows[0]["Cus_No"].ToString();
cus.Cus_Name = dsCus.Tables[0].Rows[0]["Cus_Name"].ToString();
cus.Cus_Address = dsCus.Tables[0].Rows[0]["Cus_Address"].ToString();
return cus;
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw ex;
}
}
}
}