Example #1
0
        /// <summary>
        /// 批量更新数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="dt"></param>
        /// <param name="columnUpdateExpression">更新列集合</param>
        /// <param name="columnPrimaryKeyExpression">主键列</param>
        /// <returns>受影响行</returns>
        public static int BulkUpdate <T>(this SqlConnection db, List <T> dt, Func <T, object> columnUpdateExpression, Func <T, object> columnPrimaryKeyExpression) where T : new()
        {
            if (columnPrimaryKeyExpression == null)
            {
                throw new Exception("columnPrimaryKeyExpression不能为空");
            }
            if (columnUpdateExpression == null)
            {
                throw new Exception("columnInputExpression不能为空");
            }

            var t         = new T();
            var tableName = typeof(T).Name;
            var pkObj     = columnPrimaryKeyExpression.Invoke(t);
            var pks       = Common.GetColumns(pkObj);

            if (pks.Length == 0)
            {
                throw new Exception("主键不能为空");
            }
            var obj     = columnUpdateExpression.Invoke(t);
            var columns = Common.GetColumns(obj);

            if (columns.Length == 0)
            {
                throw new Exception("更新列不能为空");
            }

            using (var sbu = new SqlBulkUpdate(db))
            {
                return(sbu.BulkUpdate(tableName, dt, pks[0], columns));
            }
        }
Example #2
0
        /// <summary>
        /// 批量更新数据(支持NotMapped属性)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TUpdateColumn"></typeparam>
        /// <typeparam name="TPkColumn"></typeparam>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="columnUpdateExpression">更新列集合</param>
        /// <param name="columnPrimaryKeyExpression">主键列</param>
        /// <param name="tran"></param>
        /// <returns>受影响行</returns>
        public static int BulkUpdate <T, TUpdateColumn, TPkColumn>(this SqlConnection db, string tableName, List <T> dt, Expression <Func <T, TUpdateColumn> > columnUpdateExpression, Expression <Func <T, TPkColumn> > columnPrimaryKeyExpression, SqlTransaction tran = null) where T : new()
        {
            if (columnPrimaryKeyExpression == null)
            {
                throw new Exception("columnPrimaryKeyExpression不能为空");
            }
            if (columnUpdateExpression == null)
            {
                throw new Exception("columnInputExpression不能为空");
            }

            var pkColumns = GetColumns(columnPrimaryKeyExpression);

            if (pkColumns.Count == 0)
            {
                throw new Exception("主键不能为空");
            }

            var updateColumns = GetColumns(columnUpdateExpression);

            if (updateColumns.Count == 0)
            {
                throw new Exception("更新列不能为空");
            }

            using (var sbu = new SqlBulkUpdate(db, tran))
            {
                return(sbu.BulkUpdate(tableName, dt, pkColumns, updateColumns));
            }
        }