/// <summary>
        /// 根据列、条件更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lstColumns"></param>
        /// <param name="lstIgnoreColumns"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool Update <T>(
            T entity,
            List <string> lstColumns       = null,
            List <string> lstIgnoreColumns = null,
            string strWhere = "") where T : class, new()
        {
            IUpdateable <T> up = _db.Updateable(entity);

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }

            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }

            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }

            return(up.ExecuteCommandHasChange());
        }
Example #2
0
        /// <summary>
        /// 根据条件更新实体信息
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lstColumns">实体列</param>
        /// <param name="lstIgnoreColumns">忽略列</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public bool Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            bool result = false;

            try
            {
                _db.Ado.BeginTran();
                IUpdateable <TEntity> up = _db.Updateable(entity);
                if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
                {
                    up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
                }
                if (lstColumns != null && lstColumns.Count > 0)
                {
                    up = up.UpdateColumns(lstColumns.ToArray());
                }
                if (!string.IsNullOrEmpty(strWhere))
                {
                    up = up.Where(strWhere);
                }
                result = up.ExecuteCommandHasChange();
                _db.Ado.CommitTran();
            }
            catch (Exception)
            {
                _db.Ado.RollbackTran();
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// 更新字段(主键)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columns"></param>
        /// <param name="whereExpression"></param>
        /// <param name="locked">是否锁</param>
        /// <returns></returns>
        public bool Update <T>(T t, Expression <Func <T, object> > columns, bool locked = false) where T : class, new()
        {
            try {
                IUpdateable <T> updateable = dal.SqlSugarDb
                                             .Updateable <T>(t)
                                             .UpdateColumns(columns);

                if (locked)
                {
                    updateable = updateable.With(SqlWith.UpdLock);
                }

                bool result = updateable.ExecuteCommandHasChange();

                return(result);
            } catch (Exception) {
                throw;
            }
        }