当前位置:编程学习 > C#/ASP.NET >>

三层架构的事务如何处理

初学asp.net,用的是3层架构,考虑到数据同步,所以想用事务,但是百度了许久,用的都是sqlhelper,目前程序并未使用sqlhelper,所以想问下DAL层的事务(非sqlhelper方式)怎么写?BLL层又如何写?

以下是我目前的部分代码。
DAL
public static void SellInsert(string sellCode, string customer, SqlTransaction tran)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
try
{
conn.Open();
//tran = conn.BeginTransaction();
SqlCommand insertcmd = new SqlCommand("insert into tb_Sell(SellCode,Customer_name) values ('" + sellCode + "'," + "'" + customerName +"')", conn);
insertcmd.ExecuteNonQuery();
}
catch (System.Exception ee)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
public static void SellDetailInsert(string sellCode, string product, SqlTransaction tran)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
try
{
conn.Open();
//tran = conn.BeginTransaction();
SqlCommand insertcmd = new SqlCommand("insert into tb_SellDetail(SellCode,Product) values ('" + sellCode + "'," + "'" + product +"')", conn);
insertcmd.ExecuteNonQuery();
}
catch (System.Exception ee)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}



BLL
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ChengYiConnectionString"].ConnectionString);
    conn.Open();
    SqlTransaction tran = conn.BeginTransaction(); 
    try
    {
DAL.Product.SellInsert(this.lblSellCode.Text,this.tbCustomerName.Text,tran);
DAL.Product.SellDetailInsert(this.lblSellCode.Text,this.tbProduct.Text,tran);
tran.Commit();
catch (Exception ex)
    {
tran.Rollback();
Response.Write(ex.Message);
}
finally
{
tran.Dispose();
conn.Close();

}
事务 asp.net 三层架构 --------------------编程问答-------------------- 建立三个项目,如项目名称为Test,那么三个项目分别为:
Test.WebClient
Test.BLL
Test.DAL

Test.WebClient不会直接访问Test.DAL,一定是通过Test.BLL去访问Test.DAL的,这正是三层架构,
也可以建附助的项目,比较辅助类的项目,实体类的项目


你的代码:
BLL 
protected void btnSave_Click(object sender, EventArgs e) 
{
}
BLL一般不会直接被前端访问,或直接访问前端




--------------------编程问答-------------------- 懒得说了,培训班误导你说把增删改查拆成三份,写三遍,就叫“三层架构”,这根本就是不对的。所谓BLL,你的Business Logic在哪里呢。你连三层都不会,谈什么三层上怎么实现“事务”。首先一个问题,你的事务属于不属于Business Logic?你连楼房都没有盖对,就问楼梯修在哪里,让人怎么回答。 --------------------编程问答-------------------- 你把你现在BLL层的方法写到DAL层里,然后BLL层里返回你新写的有事务的方法如果有不对的地方,求指教 谢谢
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,