静以思

Microsoft .NET[C#] - SQL SERVER - ORACLE - Web Design

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
  .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)");
        }

posted on 2008-02-19 17:35  plife  阅读(198)  评论(0)    收藏  举报