码迷,mamicode.com
首页 > 数据库 > 详细

C# MySql Transaction Async

时间:2019-12-10 18:36:19      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:sdi   connect   cmd   command   code   await   exce   with   llb   

 static async Task<int> MySqlWriteAsync(string writeSQL,Dictionary<string,object> parametersDic=null)
        {
            int executeResult = -1;
            using (MySqlConnection conn = GetMySqlConnection())
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }

                using (MySqlCommand cmd = new MySqlCommand(writeSQL, conn))
                {
                    using (MySqlTransaction myTrans = await conn.BeginTransactionAsync())
                    {
                        try
                        {
                            if (parametersDic != null && parametersDic.Any())
                            {
                                foreach (var pDic in parametersDic)
                                {
                                    cmd.Parameters.AddWithValue(pDic.Key, pDic.Value);
                                }
                            }

                            cmd.Transaction = myTrans;
                            executeResult = await cmd.ExecuteNonQueryAsync();
                            myTrans.Commit();
                        }
                        catch (Exception ex)
                        {
                            myTrans.Rollback();
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            return executeResult;            
        }

 

C# MySql Transaction Async

标签:sdi   connect   cmd   command   code   await   exce   with   llb   

原文地址:https://www.cnblogs.com/Fred1987/p/12018360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!