using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AcPro.DataLayer
{
///
/// Summary description for DataAccess.
///
public class DataAccess
{
protected static SqlConnection Conn;
protected static SqlTransaction sqlTrans;
public DataAccess()
{
//
// TODO: Add constructor logic here
//
}
public static void OpenConnection()
{
string strConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
Conn=new SqlConnection();
if(Conn.State==ConnectionState.Open)
{
Conn.Close();
}
else
{
Conn.ConnectionString=strConnectionString;
Conn.Open();
}
}
public static void CloseConnection()
{
if(Conn!=null)
{
if(Conn.State==ConnectionState.Open)
{
Conn.Close();
}
Conn=null;
}
}
public static void BeginTransaction()
{
OpenConnection();
//Begin the transaction
sqlTrans=Conn.BeginTransaction();
}
///
/// Commits the Transaction
///
public static void CommitTransaction()
{
sqlTrans.Commit();
sqlTrans.Dispose();
CloseConnection();
}
///
/// Rolls Back the Transaction
///
public static void RollBackTransaction()
{
sqlTrans.Rollback();
sqlTrans.Dispose();
CloseConnection();
}
public static int ExecuteNonQuery(string CommandText,SqlParameter[] Params)
{
int intRecordsAffected;
OpenConnection();
SqlCommand cmdObj=new SqlCommand();
sqlTrans=Conn.BeginTransaction();
cmdObj.CommandType=CommandType.StoredProcedure;
cmdObj.Connection=Conn;
cmdObj.CommandText=CommandText;
cmdObj.Transaction=sqlTrans;
if (Params.Length>0)
{
for (int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
try
{
intRecordsAffected=cmdObj.ExecuteNonQuery();
sqlTrans.Commit();
}
catch(SqlException e)
{
sqlTrans.Rollback();
throw e;
}
cmdObj.Dispose();
CloseConnection();
return intRecordsAffected;
}
public static int ExecutiveSclar(string CommandText, SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if(Params.Length>0)
{
for(int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
int HeadPk=Convert.ToInt32(cmdObj.ExecuteScalar());
cmdObj.Dispose();
CloseConnection();
return HeadPk;
}
///
/// return values in dataset
///
///
///
///
public static DataSet ExecuteSql(string CommandText, SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if(Params.Length>0)
{
for(int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
DataSet myDS=new DataSet();
mySDA.Fill(myDS);
//cmdObj.Dispose();
CloseConnection();
return myDS;
}
public static DataTable populateledger(String CommandText,SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if (Params.Length>0)
{
for (int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
DataTable myDT=new DataTable();
myDT.Clear();
mySDA.Fill(myDT);
mySDA.Dispose();
// cmdObj.Dispose();
CloseConnection();
return myDT;
}
public static DataTable ExecuteTable(string strsql)
{OpenConnection();
DataTable dt=new DataTable();
try
{
SqlDataAdapter da=new SqlDataAdapter(strsql,Conn);
da.Fill(dt);
}
catch(Exception e)
{
throw e;
}
return dt;
}
}
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AcPro.DataLayer
{
///
/// Summary description for DataAccess.
///
public class DataAccess
{
protected static SqlConnection Conn;
protected static SqlTransaction sqlTrans;
public DataAccess()
{
//
// TODO: Add constructor logic here
//
}
public static void OpenConnection()
{
string strConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
Conn=new SqlConnection();
if(Conn.State==ConnectionState.Open)
{
Conn.Close();
}
else
{
Conn.ConnectionString=strConnectionString;
Conn.Open();
}
}
public static void CloseConnection()
{
if(Conn!=null)
{
if(Conn.State==ConnectionState.Open)
{
Conn.Close();
}
Conn=null;
}
}
public static void BeginTransaction()
{
OpenConnection();
//Begin the transaction
sqlTrans=Conn.BeginTransaction();
}
///
/// Commits the Transaction
///
public static void CommitTransaction()
{
sqlTrans.Commit();
sqlTrans.Dispose();
CloseConnection();
}
///
/// Rolls Back the Transaction
///
public static void RollBackTransaction()
{
sqlTrans.Rollback();
sqlTrans.Dispose();
CloseConnection();
}
public static int ExecuteNonQuery(string CommandText,SqlParameter[] Params)
{
int intRecordsAffected;
OpenConnection();
SqlCommand cmdObj=new SqlCommand();
sqlTrans=Conn.BeginTransaction();
cmdObj.CommandType=CommandType.StoredProcedure;
cmdObj.Connection=Conn;
cmdObj.CommandText=CommandText;
cmdObj.Transaction=sqlTrans;
if (Params.Length>0)
{
for (int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
try
{
intRecordsAffected=cmdObj.ExecuteNonQuery();
sqlTrans.Commit();
}
catch(SqlException e)
{
sqlTrans.Rollback();
throw e;
}
cmdObj.Dispose();
CloseConnection();
return intRecordsAffected;
}
public static int ExecutiveSclar(string CommandText, SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if(Params.Length>0)
{
for(int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
int HeadPk=Convert.ToInt32(cmdObj.ExecuteScalar());
cmdObj.Dispose();
CloseConnection();
return HeadPk;
}
///
/// return values in dataset
///
///
///
///
public static DataSet ExecuteSql(string CommandText, SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if(Params.Length>0)
{
for(int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
DataSet myDS=new DataSet();
mySDA.Fill(myDS);
//cmdObj.Dispose();
CloseConnection();
return myDS;
}
public static DataTable populateledger(String CommandText,SqlParameter[] Params)
{
OpenConnection();
SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
cmdObj.CommandType=CommandType.StoredProcedure;
if (Params.Length>0)
{
for (int i=0;i
{
cmdObj.Parameters.Add(Params[i]);
}
}
SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
DataTable myDT=new DataTable();
myDT.Clear();
mySDA.Fill(myDT);
mySDA.Dispose();
// cmdObj.Dispose();
CloseConnection();
return myDT;
}
public static DataTable ExecuteTable(string strsql)
{OpenConnection();
DataTable dt=new DataTable();
try
{
SqlDataAdapter da=new SqlDataAdapter(strsql,Conn);
da.Fill(dt);
}
catch(Exception e)
{
throw e;
}
return dt;
}
}
}
No comments:
Post a Comment