using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
namespace MFDataLogic
{
public class DataLayerLogic
{
SqlDataAdapter da;
SqlCommand cmd;
SqlConnection con;
DataSet ds;
DataTable dt;
//String ConStr = "Data Source=Iwc;Initial Catalog=iwc;Integrated Security=True;";
String ConStr = System.Configuration.ConfigurationSettings.AppSettings["conMF"];
//Open Connection
private void openConnection()
{
try
{
if (ConStr == "")
{
throw new Exception("Connection String is not found in application Configuration file");
}
else if (con == null)
{
con = new SqlConnection(ConStr);
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
catch (SqlException ex)
{
throw (ex);
}
}
//Close Connection
private void closeConnection()
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
//Dispose connection
private void disposeConnection()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
//Get DataSet that accept procedure having no parameter
public DataSet getDataSetWProcedureNoParameter(string ProcedureStr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
cmd = new SqlCommand();
ds = new DataSet();
da = new SqlDataAdapter();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(ds);
trans.Commit();
closeConnection();
disposeConnection();
return ds;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
//Get DataSet that accept procedure having parameter
public DataSet getDataSetWProcedure(Hashtable HT, string ProcedureStr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
cmd = new SqlCommand();
ds = new DataSet();
da = new SqlDataAdapter();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in HT.Keys)
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), HT[myKey].ToString().Trim());
}
cmd.Prepare();
da.SelectCommand = cmd;
da.Fill(ds);
trans.Commit();
closeConnection();
disposeConnection();
return ds;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
//Execute Procedure with parameter
public int executeQueryWProcedure(Hashtable HT, string ProcedureStr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
cmd = new SqlCommand();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in HT.Keys)
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), HT[myKey].ToString().Trim());
}
cmd.Prepare();
int result = cmd.ExecuteNonQuery();
trans.Commit();
closeConnection();
disposeConnection();
return result;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
//Execute DataReader with Procedure having no parameter
public SqlDataReader executeReaderWProcedureNoParameter(String ProcedureStr)
{
try
{
openConnection();
cmd = new SqlCommand();
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (SqlException ex)
{
throw (ex);
}
}
//Execute DataReader with Procedure having parameter
public SqlDataReader executeReaderWProcedure(Hashtable HT, String ProcedureStr)
{
try
{
openConnection();
cmd = new SqlCommand();
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in HT.Keys)
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), HT[myKey].ToString().Trim());
}
cmd.Prepare();
SqlDataReader dr;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (SqlException ex)
{
throw (ex);
}
}
// Close sqlDataReader
public void closeDataReader(SqlDataReader dr)
{
if (dr.IsClosed == false)
{
dr.Close();
}
}
//Get DataTable with procedure having no parameter
public DataTable getDataTableWProcedureNoParameter(String ProcedureStr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
dt = new DataTable();
da = new SqlDataAdapter();
cmd = new SqlCommand();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
trans.Commit();
closeConnection();
disposeConnection();
return dt;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
//Get DataTable with procedure having parameter
public DataTable getDataTableWProcedure(Hashtable HT, String ProcedureStr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
dt = new DataTable();
da = new SqlDataAdapter();
cmd = new SqlCommand();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in HT.Keys)
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), HT[myKey].ToString().Trim());
}
cmd.Prepare();
da.SelectCommand = cmd;
da.Fill(dt);
trans.Commit();
closeConnection();
disposeConnection();
return dt;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
//Execute Two procedure to work in same Command
public int executeQueryWProcedure1(Hashtable HT, string ProcedureStr, Hashtable LoopHT, string ProcedureStrLoop, ArrayList Arr)
{
SqlTransaction trans = null;
try
{
openConnection();
trans = con.BeginTransaction();
cmd = new SqlCommand();
cmd.Transaction = trans;
cmd.CommandText = ProcedureStr;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in HT.Keys)
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), HT[myKey].ToString().Trim());
}
cmd.Prepare();
int result = cmd.ExecuteNonQuery();
Int32 LoopCount = Arr.Count;
for (Int32 i = 0; i < LoopCount; i++)
{
cmd.Parameters.Clear();
cmd.CommandText = ProcedureStrLoop;
cmd.Connection = con;
cmd.CommandTimeout = 90;
cmd.CommandType = CommandType.StoredProcedure;
foreach (string myKey in LoopHT.Keys)
{
if (LoopHT[myKey].ToString().Trim() == "1*$#^@&!9")
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), Arr[i].ToString().Trim());
}
else
{
cmd.Parameters.AddWithValue(myKey.ToString().Trim(), LoopHT[myKey].ToString().Trim());
}
}
cmd.Prepare();
result = cmd.ExecuteNonQuery();
}
trans.Commit();
cmd.Dispose();
closeConnection();
disposeConnection();
return result;
}
catch (SqlException ex)
{
trans.Rollback();
throw (ex);
}
}
}
}
0 comments:
Post a Comment