zoukankan      html  css  js  c++  java
  • Mysql事务代码

            /// <summary>
            /// 删除相册
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public bool DeleAblumsById(long id)
            {
                int photo_Result = 0, ablums_Result = 0;
                string strSql_Photo = "delete from tb_photo where albumsId=" + id;
                string strSql_Ablums = "delete from tb_albums where id=" + id;
                MySqlTransaction tran = null;
                MySqlCommand com = null;
                MySqlConnection conn = null;
                try
                {
                    conn = new MySqlConnection(_ConnectionString);
                    conn.Open();
                    tran = conn.BeginTransaction();
                    com = new MySqlCommand(strSql_Photo, conn, tran);
                    photo_Result = com.ExecuteNonQuery();
    
                    com = new MySqlCommand(strSql_Ablums, conn, tran);
                    ablums_Result = com.ExecuteNonQuery();
                    if (photo_Result >= 0 && ablums_Result > 0)
                        tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                }
                finally
                {
                    tran.Dispose();
                    conn.Close();
                }
                return photo_Result >= 0 && ablums_Result > 0;
            }
    public static int ExecuteNonQuery(List<KeyValuePair<String, IEnumerable<MySqlParameter>>> strList, string connectionString)
            {
                int count = 0;
                using (MySqlConnection con = new MySqlConnection(connectionString))
                {
                    con.Open();
                    MySqlTransaction tran = con.BeginTransaction();
                    MySqlCommand comm = new MySqlCommand();
                    comm.Connection = con;
                    comm.Transaction = tran;
                    try
                    {
                        strList.ForEach(o =>
                        {
                            comm.CommandText = o.Key;
                            IEnumerable<MySqlParameter> para = o.Value;
                            if (para != null)
                            {
                                foreach (var pa in para)
                                    comm.Parameters.Add(pa);
                            }
                            count += comm.ExecuteNonQuery();
                            comm.Parameters.Clear();
                        });
                        tran.Commit();
                        return count;
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return count;
                    }
                }
            }
  • 相关阅读:
    函数宏实现循环左移
    函数宏判断小端
    Linux下32位与64位数据类型大小
    转:C语言嵌入式系统编程之软件架构篇
    转:详解大端小段模式
    time函数计算时间
    匈牙利命名法
    20131030
    20131029
    20131028
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3542888.html
Copyright © 2011-2022 走看看