三层架构的事务如何处理
初学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 在三层架构中事务的使用实例代码
接触3层也有一段时间了,了解水平一般,前段时间在想在三层中怎么使用事务呢,放在哪呢?Sqlherper ? DAL? BLL?。然后我就疯狂的百度,好几次都是未果(因为做的都是小项目,不用事务也关系不大),今天我再次查时,好好的看了csdn上的以讨论,http://topic.csdn.net/u/20091101/19/f21697d7-8f0c-4eb3-8e59-d0fe2f0b04b0.html,结合前辈和高手们的意见,自己改了一个出来。我的想法是将事务逻辑写在业务逻辑层,数据库的处理还都是在SQLHELPER,BLL层通过事务SqlTransaction传值访问DAL,再访问Sqlh
JavaWeb商城项目全解析:从三层架构到订单事务处理
JavaWeb开发是企业级应用构建的核心技术领域,其基础建立在Servlet、JSP与JDBC等技术组件之上,通过MVC设计模式实现业务逻辑与表现层的分离。理解三层架构(表现层、业务逻辑层、数据访问层)的原理,是掌握Web应用数据流转与模块解耦的关键,这直接提升了系统的可维护性与扩展性。在电商等高并发场景中,数据库事务管理技术尤为重要,它确保了如库存扣减、订单生成等核心操作的原子性与一致性,是保障业务数据准确性的基石。本文以典型的JavaWeb商城项目为具体载体,深入剖析其如何运用这些基础技术实现用户管理、
Delphi 三层架构
Delphi 多层系统开发,多层架构,MTS。
C#窗体程序对数据库的增删改查操作(三层架构).
基于vb进行数据库的增删改查操作 sql语句 c#
Java jdbc三层及事务(转账案例)
通过转账案例讲解了三层和事务,以及容易出现问题的关键点


