.net 2.0 framework 中新增了 System.Transactions 命名空间,其中提供的一系列接口和类使得在.net 2.0 中使用事务比起从前要方便了许多。有关在 .net 2.0 下操作数据库事务的文章已经有了很多,这里只提一下如何设计自定义事务操作。
相关实例代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
GvBind();
}
private void GvBind()
{
this.GridView1.DataSource = DbHelperSQL.Query(\"SELECT * FROM Items\");
this.GridView1.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// 声明定义事务
using (TransactionScope ts = new TransactionScope())
{
try
{
// 事务方法体
Sub1();
Sub2();
// 提交事务并回滚
ts.Complete();
}
catch
{ } [Page]
}
this.GvBind();
}
private static void Sub2()
{
DbHelperSQL.ExecuteSql(\"INSERT INTO Items(Item) VALUES(’aa’)\");
}
private static void Sub1()
{
DbHelperSQL.ExecuteSql(\"INSERT INTO Items(Item) VALUES(101)\");
}