Example #1
0
        /// <summary>
        /// 更新某些列
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lstColumns">列集合</param>
        /// <param name="lstIgnoreColumns">忽略的列集合</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns></returns>
        public async Task <bool> Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            //IUpdateable<TEntity> up = await Task.Run(() => m_db.Updateable(entity));
            //if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            //{
            //    up = await Task.Run(() => up.IgnoreColumns(it => lstIgnoreColumns.Contains(it)));
            //}
            //if (lstColumns != null && lstColumns.Count > 0)
            //{
            //    up = await Task.Run(() => up.UpdateColumns(it => lstColumns.Contains(it)));
            //}
            //if (!string.IsNullOrEmpty(strWhere))
            //{
            //    up = await Task.Run(() => up.Where(strWhere));
            //}
            //return await Task.Run(() => up.ExecuteCommand()) > 0;

            IUpdateable <TEntity> up = m_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(await up.ExecuteCommandHasChangeAsync());
        }
Example #2
0
        /// <summary>
        /// 根据条件更新实体信息(异步)
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lstColumns">实体列</param>
        /// <param name="lstIgnoreColumns">忽略列</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public async Task <bool> T_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 = await up.ExecuteCommandHasChangeAsync();

                _db.Ado.CommitTran();
            }
            catch (Exception)
            {
                _db.Ado.RollbackTran();
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// 根据条件更新实体信息(返回受影响行数)
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lstColumns">实体列</param>
        /// <param name="lstIgnoreColumns">忽略列</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int UpdateEx(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            int result = 0;

            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.ExecuteCommand();
                _db.Ado.CommitTran();
            }
            catch (Exception)
            {
                _db.Ado.RollbackTran();
            }
            return(result);
        }
        /// <summary>
        /// 根据列、条件更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lstColumns"></param>
        /// <param name="lstIgnoreColumns"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public async Task <bool> UpdateAsync <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(await up.ExecuteCommandHasChangeAsync());
        }
Example #5
0
        /// <summary>
        /// 编辑
        /// </summary>
        /// <typeparam name="T">表实体</typeparam>
        /// <param name="entity">实体内容</param>
        /// <param name="columns">要编辑的列</param>
        /// <param name="where">条件</param>
        /// <returns>受影响行数</returns>
        public int Edit <T>(T entity, Expression <Func <T, T> > columns, Expression <Func <T, bool> > where) where T : class, new()
        {
            IUpdateable <T> r = _db.Updateable(entity);

            if (columns != null)
            {
                r = r.UpdateColumns(columns);
            }

            return(r.Where(where).ExecuteCommand());
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T">表实体</typeparam>
        /// <param name="entity"></param>
        /// <param name="fileds">需要更新的字段</param>
        /// <param name="where">条件</param>
        /// <returns>受影响行数</returns>
        public int Update <T>(T entity, Expression <Func <T, object> > fileds = null
                              , Expression <Func <T, bool> > where            = null) where T : class, new()
        {
            IUpdateable <T> upd = _db.Updateable(entity);

            if (fileds != null)
            {
                upd.UpdateColumns(fileds);
            }
            if (where != null)
            {
                upd.Where(where);
            }
            return(upd.ExecuteCommand());
        }
Example #7
0
        public async Task <int> Update(TEntity model, List <string> columns = null, List <string> ignoreColumn = null, string where = null)
        {
            IUpdateable <TEntity> up = await Task.Run(() => Db.Updateable <TEntity>(model));

            if (columns != null && columns.Count > 0)
            {
                up = await Task.Run(() => up.UpdateColumns(columns.ToArray()));
            }
            if (ignoreColumn != null && ignoreColumn.Count > 0)
            {
                up = await Task.Run(() => up.IgnoreColumns(ignoreColumn.ToArray()));
            }
            if (string.IsNullOrWhiteSpace(where))
            {
                up = await Task.Run(() => up.Where(where));
            }
            return(await up.ExecuteCommandAsync());
        }
Example #8
0
        /// <summary>
        /// 更新 忽略更新某些列 及更新某些列 通过条件判断
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lstColumns"></param>
        /// <param name="lstIgnoreColumns"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public async Task <bool> Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            IUpdateable <TEntity> up = await Task.Run(() => db.Updateable(entity));

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = await Task.Run(() => up.IgnoreColumns(lstIgnoreColumns.ToArray()));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = await Task.Run(() => up.UpdateColumns(lstColumns.ToArray()));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = await Task.Run(() => up.Where(strWhere));
            }
            return(await Task.Run(() => up.ExecuteCommand()) > 0);
        }
Example #9
0
        public bool Update(T model, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            IUpdateable <T> up = db.Updateable(model);

            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.ExecuteCommand() > 0);
        }
Example #10
0
        public async Task <bool> Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            //throw new NotImplementedException();
            IUpdateable <TEntity> up = await Task.Run(() => db.Updateable(entity));

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = await Task.Run(() => up.IgnoreColumns(it => lstIgnoreColumns.Contains(it)));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = await Task.Run(() => up.UpdateColumns(it => lstColumns.Contains(it)));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = await Task.Run(() => up.WhereColumns(strWhere));
            }
            return(await Task.Run(() => up.ExecuteCommand()) > 0);
        }
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="lstColumns">排除列集合</param>
        /// <param name="lstIgnoreColumns">更新列集合(放对应的更新列就好)</param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public async Task <bool> Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            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);
            }
            return(await up.ExecuteCommandHasChangeAsync());
        }
Example #12
0
        /// <summary>
        /// 根据指定条件更新指定列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lstColumns"></param>
        /// <param name="lstIgnoreColumns"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public async Task <bool> Update(TEntity entity, List <string> lstColumns = null, List <string> lstIgnoreColumns = null, string strWhere = "")
        {
            IUpdateable <TEntity> up = _db.Updateable(entity);

            if (lstColumns?.Any() == true)
            {
                up.UpdateColumns(lstColumns.ToArray());
            }

            if (lstIgnoreColumns?.Any() == true)
            {
                up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }

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

            return(await up.ExecuteCommandHasChangeAsync());
        }
Example #13
0
        /// <summary>
        ///     更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="columns">更新的列</param>
        /// <param name="ignoreColumns">忽略的列</param>
        /// <param name="strWhere">更新条件</param>
        /// <returns></returns>
        public virtual async Task <bool> Update(T entity, List <string> columns = null, List <string> ignoreColumns = null,
                                                string strWhere = "")
        {
            IUpdateable <T> updateable = this.SugarClient.Updateable(entity);

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

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

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

            return(await updateable.ExecuteCommandHasChangeAsync());
        }
Example #14
0
        /// <summary>
        /// 功能描述:修改数据
        /// </summary>
        /// <param name="实体">entity</param>
        /// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>是否成功</returns>
        public bool Update(
            T entity,
            List <string> lstColumns,
            List <string> lstIgnoreColumns,
            Expression <Func <T, bool> > whereExpression)
        {
            IUpdateable <T> up = _db.Updateable(entity);

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return(up.ExecuteCommand() > 0);
        }
Example #15
0
        /// <summary>
        /// 功能描述:修改数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="strWhere">条件</param>
        /// <returns>是否成功</returns>
        public bool Update(
            T entity,
            List <string> lstColumns,
            List <string> lstIgnoreColumns,
            string strWhere)
        {
            IUpdateable <T> up = _db.Updateable(entity);

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return(up.ExecuteCommand() > 0);
        }
Example #16
0
        /// <summary>
        /// 功能描述:修改数据
        /// </summary>
        /// <param name="entitys">实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
        /// <param name="lstColumns">更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="lstIgnoreColumns">不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)</param>
        /// <param name="strWhere">条件</param>
        /// <param name="lstParameters">条件参数</param>
        /// <returns>受影响行数</returns>
        public int Update(
            T[] entitys,
            List <string> lstColumns,
            List <string> lstIgnoreColumns,
            string strWhere,
            List <SugarParameter> lstParameters)
        {
            IUpdateable <T> up = _db.Updateable(entitys);

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere, lstParameters);
            }
            return(up.ExecuteCommand());
        }
Example #17
0
        public static async Task <bool> Update <TEntity>(
            this SqlSugarClient db,
            TEntity entity,
            List <string> lstColumns       = null,
            List <string> lstIgnoreColumns = null,
            string strWhere = ""
            ) where TEntity : EntityBase, new()
        {
            IUpdateable <TEntity> up = await Task.Run(() => db.Updateable(entity));

            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = await Task.Run(() => up.IgnoreColumns(it => lstIgnoreColumns.Contains(it)));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = await Task.Run(() => up.UpdateColumns(it => lstColumns.Contains(it)));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = await Task.Run(() => up.Where(strWhere));
            }
            return(await Task.Run(() => up.ExecuteCommand()) > 0);
        }