A Simple DAL layer to access SQL Server with transaction
support.
The usage of the above class is given below
support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DbLayer
{
public class TSQLAccess
{
private SqlConnection _con = null;
private SqlCommand _cmd = null;
string _constr;
SqlTransaction _tran;
public TSQLAccess(string constr)
{
_constr = constr;
}
public bool Open()
{
_con = new SqlConnection(_constr);
_con.Open();
_tran = _con.BeginTransaction();
return true;
}
public bool Close()
{
_tran.Commit();
_con.Close();
return true;
}
public bool Abort()
{
_tran.Rollback();
_con.Close();
return true;
}
public DataSet Execute(string SQL)
{
_cmd = new SqlCommand(SQL, _con);
_cmd.Transaction = _tran;
SqlDataAdapter da = new SqlDataAdapter(_cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public SqlDataReader ExecuteQuery(string SQL)
{
_cmd = new SqlCommand(SQL, _con);
_cmd.Transaction = _tran;
SqlDataReader rs = _cmd.ExecuteReader();
return rs;
}
public bool ExecuteNonQuery(string SQL)
{
try
{
_cmd = new SqlCommand(SQL, _con);
_cmd.Transaction = _tran;
_cmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
e.ToString();
return false;
}
}
}
}
The usage of the above class is given below
public class Journal
{
public string jid;
public string jdate;
public string narration;
public List<JournalEntry> rs;
}
public class JournalEntry
{
public string jcode;
public string jname;
public string drcr;
public string amount;
}
private static bool DoPost(Journal j)
{
TSQLAccess sq = new TSQLAccess(DbConfig.GetConStr("MAINDB"));
try
{
sq.Open();
string qry = "insert into JournalMaster values('" + j.jid + "','JV',
cast('"+DateTime.Now.ToLongDateString()+ "' as datetime),0,'" + j.narration + "')";
if (!sq.ExecuteNonQuery(qry))
{
return false;
}
List<JournalEntry> je = j.rs;
foreach (JournalEntry js in je)
{
qry = "insert into JournalDetail values('" + j.jid + "','" +
js.jcode + "','" + js.drcr + "'," + js.amount + ")";
sq.ExecuteNonQuery(qry);
}
sq.Close();
return true;
}
catch (Exception ex)
{
sq.Abort();
return false;
}
}
1 comment:
I used to be able to find good advice from your articles.
Here is my web-site - http://green-smoke.yolasite.com/
My web page: http://green-smoke.yolasite.com/
Post a Comment