叶祖辉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
复制代码
public static void Transaction()
{
    myitEntities entity = null;
    DbTransaction tran = null;
    try
    {
    entity = new myitEntities();
    entity.Connection.Open();
    tran = entity.Connection.BeginTransaction();
    Student st = entity.Student.FirstOrDefault(c => c.StudentID == 20);
    st.StudentName = "test";
    st.Age = 55;
    entity.SaveChanges();
    // 提交事务
    tran.Commit();

    }
    catch (Exception ex)
    {
    if (tran != null)
    {
        // 事务回滚
        tran.Rollback();
        Console.WriteLine("事务回滚");
        throw ex;
    }
    }
    finally {
    if (entity != null && entity.Connection.State != ConnectionState.Closed)
    {
        entity.Connection.Close();
    }
    }
}
复制代码
posted on 2017-05-11 15:52  叶祖辉  阅读(177)  评论(0编辑  收藏  举报
友情链接:回力鞋官网 | 中老年女装 | 新航道托福 | 十月英文 | 电影推荐