[EF]事务处理

EF支持事务处理操作,以下语句会获取当前DbContext对象专用的DbContextTransaction()事务处理对象

DbContextTransaction transaction=context.Database.BeginTransaction();
获取transaction之后,期间任何与context有关的数据操作均会纳入此事务处理的范围中,调用transaction.Commit方法即可完成事务处理,若要放弃事务处理,则调用transaction.Rollback方法即可达到目的。
//using TransactionsDemo.Model;

        using (KTStoreContext context = new KTStoreContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var products = context.Products;
                        foreach (Product product in products)
                        {
                            int p = (int)(product.Price * 0.5);
                            product.SPrice = p;
                        }
                        int c = context.SaveChanges();
                        transaction.Commit();
                        Console.WriteLine("更新了 {0} 项数据", c);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Console.WriteLine("事务处理失败");
                    }
                }
            }
            Console.ReadKey();
展开阅读全文

事务处理

10-15

一个PostButton按钮的Click事件里面对2个表的插入操作rn如果是这种情况,rn我应该怎么进行事务处理?rn还是各位有什么好方法可以解决这个问题/rnrn程序如下rnprivate void PostButton_Click(object sender, rnSystem.EventArgs e)rnrnrn //表1(类Article )rn Article _ar = new Articlern(strConn,iOperatorID);rn st_Article _sar=new st_Articlern();rnrn _sar.ar_Title =dr_TitleTextbox.Text.Trimrn();rn _sar.ar_SubTitle = rndr_SubTitleTextbox.Text.Trim();rn _sar.ar_Source = rndr_SourceTextbox.Text.Trim();rn _sar.ar_Author = rndr_AuthorTextbox.Text.Trim();rn _sar.ar_KeyWord = rndr_KeyWordTextbox.Text.Trim();rn _sar.ar_Content = rndr_ContentTextbox.Text.Trim();rn rn st_ColnArticlec _car = new rnst_ColnArticlec();rn _car.co_ID = int.Parsern(ColumnDropDownList.SelectedItem.Value.ToString());rn rn _ar.InsArticle(_sar,_car);rn rn //表2(类AuditLog )rn AuditLog _au = new AuditLogrn(strConn,iOperatorID);rn st_AuditLog _sau =new rnst_AuditLog();rn rn _sau.ar_ID = t_dr.dr_ID;rn _sau.us_ID = iOperatorID;rn _sau.al_Event = "送审";rnrn _sau.co_ID = t_Ina.co_ID;rn rn _au.InsAuditLog(_sau);rnrnrnrn//=========================================================rn=========================================//rn(类Article)rnpublic class Articlern rn public bool InsArticle(st_Article rn_sar,st_ColnArticlec _scar)rn rn tryrn rnrnrn OleDbConnection conn rn= new OleDbConnection(strConn);rn conn.Open();rnrn OleDbDataAdapter da = rnnew OleDbDataAdapterrn("p_Ins_ArticleColnArticleAuditLog",conn);rnrn rn da.SelectCommand.CommandType = rnCommandType.StoredProcedure;rnrn rn da.SelectCommand.Parameters.Add(OperatorID_PARM, rnOleDbType.Integer).Value =iOperatorID;rnrn rn da.SelectCommand.Parameters.Add(ar_Title_PARM, rnOleDbType.VarChar,60).Value = _sar.ar_Title ;rn rn da.SelectCommand.Parameters.Addrn(ar_SubTitle_PARM, OleDbType.VarChar,60).Value = rn_sar.ar_SubTitle ;rn rn da.SelectCommand.Parameters.Add(ar_Source_PARM, rnOleDbType.VarChar,256).Value = _sar.ar_Source ;rn rn da.SelectCommand.Parameters.Add(ar_Author_PARM, rnOleDbType.VarChar,20).Value = _sar.ar_Author ;rn rn da.SelectCommand.Parameters.Add(ar_KeyWord_PARM, rnOleDbType.VarChar,40).Value = _sar.ar_KeyWord ;rn rn da.SelectCommand.Parameters.Add(ar_Content_PARM, rnOleDbType.VarChar,256).Value = _sar.ar_Content ;rn rn da.SelectCommand.Parameters.Add(co_ID_PARM, rnOleDbType.Integer).Value = _scar.co_ID;rn rn rn da.SelectCommand.Parameters.Addrn(al_Event_PARM,OleDbType.Char,10).Value = "签发";rn rnrn rn da.SelectCommand.ExecuteNonQuery();rn rn rn return true;rn rn catchrn rn return false;rn rnrn rnrn//=========================================================rn====================//rn//类AuditLogrnpublic class AuditLogrnrn public bool InsAuditLog(st_AuditLog _au)rn rn tryrn rnrnrn OleDbConnection conn rn= new OleDbConnection(strConn);rn conn.Open();rnrn OleDbDataAdapter da = rnnew OleDbDataAdapter("p_Ins_AuditLog",conn);rnrn rn da.SelectCommand.CommandType = rnCommandType.StoredProcedure;rnrn rn da.SelectCommand.Parameters.Add(OperatorID_PARM, rnOleDbType.Integer).Value =iOperatorID;rn rn rn da.SelectCommand.Parameters.Add(ar_ID_PARM, rnOleDbType.Integer).Value =_au.ar_ID ;rn rn da.SelectCommand.Parameters.Add(co_ID_PARM, rnOleDbType.Integer).Value =_au.co_ID ;rn rn da.SelectCommand.Parameters.Add(us_ID_PARM, rnOleDbType.Integer).Value =_au.us_ID;rn rn da.SelectCommand.Parameters.Addrn(al_Event_PARM,OleDbType.Char,10).Value =_au.al_Event;rnrn rn da.SelectCommand.ExecuteNonQuery();rn rn return true;rn rn catchrn rn return false;rn rnrn rnrn上边只是一种简单情况,现在不一定是只有两个表,也可能有多个rn表,而且能对一个表进行多次插入(插入次数不定)rn 论坛

没有更多推荐了,返回首页