C#中事务的用法。

using (var conn = new SqlConnection(connectionString))
            {
                conn.Open(); 
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        flag = await conn.ExecuteAsync(Applysql, Item, tran) > 0;
                        if (Item.Status==1)
                        {
                            flag = await conn.ExecuteAsync(Usersql, Item, tran) > 0;
                            var list = await userRepository.Get(new User() { Id = Item.Creater });
                            String hingeString = JsonConvert.SerializeObject(list);
                            flag = GetuiHelper.Send(Item.Creater, hingeString);
                           
                        }
                        flag = await conn.ExecuteAsync(Enterprisesql, Item, tran) > 0;
                        tran.Commit();提交事务
                        conn.Close();
                    }
                    catch (Exception ex)
                    {
                        flag = false;
                        tran.Rollback();回滚事务
                        conn.Close();
                    }
                }
            }
上一篇:sql语句中begin tran...commit tran...rollback tran


下一篇:C# MySql 批量插入数据 MySqlBulkLoader