Esempio n. 1
0
        /// <summary>
        /// 新增、修改、删除操作
        /// </summary>
        /// <param name="sqlStr">sql字符</param>
        /// <returns>无</returns>
        public static void insOrUpdOrDel(string sqlStr)
        {
            //创建数据库连接
            MySqlConnection conn = new MySqlConnect().getConnection(new MySqlConnect().connection());
            //启动一个事务
            MySqlTransaction myTrans = conn.BeginTransaction();
            //事务命令
            MySqlCommand cmd = conn.CreateCommand();

            //事务命令
            cmd.Transaction = myTrans;
            try
            {
                //执行数据库新增、修改、删除操作
                MySqlHelper.ExecuteNonQuery(conn, sqlStr);
                //事务提交
                myTrans.Commit();
                //关闭释放连接资源
                conn.Close();
            }
            catch (Exception ex)
            {
                //遇到错误回滚
                myTrans.Rollback();
                //关闭释放连接资源
                conn.Close();
                //错误日志
                LogHelper.WriteError("新增、修改、删除操作异常回滚:" + ex.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 数据库参数式新增、修改、删除操作
        /// </summary>
        /// <param name="sqlStr">sql字符</param>
        /// <returns>无</returns>
        public static int insOrUpdOrDel(string sqlStr, MySqlParameter[] commandParameters)
        {
            //创建数据库连接
            MySqlConnection conn = new MySqlConnect().getConnection(new MySqlConnect().connection());
            //启动一个事务
            MySqlTransaction myTrans = conn.BeginTransaction();
            //事务命令
            MySqlCommand cmd = conn.CreateCommand();

            //事务命令
            cmd.Transaction = myTrans;
            int result = 0;

            try
            {
                //执行数据库新增、修改、删除操作
                result = MySqlHelper.ExecuteNonQuery(conn, sqlStr, commandParameters);
                //事务提交
                myTrans.Commit();
                //关闭释放连接资源
                conn.Close();
                //
                return(result);
            }
            catch (Exception ex)
            {
                //异常回滚
                myTrans.Rollback();
                //关闭释放连接资源
                conn.Close();
                //异常日志
                LogHelper.WriteError("新增、修改、删除操作异常回滚:" + ex.Message);
                return(0);
            }
        }