您的位置:首页 > 其它

在.NET 2.0 中使用自定义事务操作

2008-03-11 10:14 267 查看
 .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
{ }
}

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)");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: