Exemplo n.º 1
0
        /// <summary>
        /// 事务下操作多个sql返回true or false
        /// </summary>
        /// <returns></returns>
        public static bool Execute(List <KeyValuePair <string, object> > vals)
        {
            //ar result = -1;

            using (MySqlConnection conn = DapperFactory.CreateMySqlConnection())
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }

                using (var trans = conn.BeginTransaction())
                {
                    foreach (var val in vals)
                    {
                        try
                        {
                            conn.Execute(val.Key, val.Value, trans, 30, CommandType.Text);
                        }
                        catch (Exception e)
                        {
                            //if (result <= 0)
                            trans.Rollback();
                            return(false);
                        }
                    }

                    trans.Commit();
                }

                if (conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }

            return(true);
        }