Beispiel #1
0
        /// <summary>
        /// 查询,按照所有不为空的条件查询
        /// </summary>
        /// <param name="Filter">查询类</param>
        /// <param name="TInfo">返回类型</param>
        /// <param name="TFilter">查询类型</param>
        /// <returns></returns>
        public List <TableEntityBase> SelectList(TableEntityBase Filter, Type TInfo, Type TFilter)
        {
            List <TableEntityBase> res     = new List <TableEntityBase>();
            TableEntityBase        refinfo = TableEntityBase.Create(TInfo);

            PropertyInfo[] Allprop = Filter.GetAllSelectField().Where(a =>
            {
                return(a.GetValue(Filter, null) != null);
            }).ToArray();
            IDbDataParameter[] dbprams = Allprop.Select(a =>
            {
                IDbDataParameter p = GetDbDataParameter(a, Filter);
                return(p);
            }).ToArray();
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                string tablename       = refinfo.GetTableName();
                string select          = string.Join(",", refinfo.GetAllField().Select(a => GetFieldName(a.Name)));
                string where           = string.Join(" and ", Allprop.Select(a => a.Name + "=" + GetPramCols(a.Name)));
                _dbCommand.CommandText = string.Format(_select, select, tablename, where.IsEmpty(" 1=1 "));
                using (DbDataReader reader = _dbCommand.ExecuteReader())
                {
                    res = this.ReaderToEntity(TInfo, reader).ToList();
                }
            });
            res.ForEach(a => a = _SelectChildren(a));
            return(res);
        }
Beispiel #2
0
        /// <summary>
        /// 删除,按照非null的进行删除,全null抛出异常
        /// </summary>
        /// <exception cref="DataBaseException"></exception>
        /// 没有删除条件
        /// <param name="info"></param>
        /// <returns></returns>
        public int DeleteList(TableEntityBase info)
        {
            int res = 0;

            PropertyInfo[] Allprop = info.GetAllSelectField().Where(a =>
            {
                return(a.GetValue(info, null) != null);
            }).ToArray();
            IDbDataParameter[] dbprams = Allprop.Select(a =>
            {
                IDbDataParameter p = GetDbDataParameter(a, info);
                return(p);
            }).ToArray();
            string tablename = info.GetTableName();

            string where = string.Join(" and ", Allprop.Select(a => a.Name + "=" + GetPramCols(a.Name)));
            if (string.IsNullOrEmpty(where))
            {
                throw new DataBaseException("没有删除条件,不能删除");
            }
            RunSql(_dbCommand =>
            {
                _dbCommand.Parameters.Clear();
                _dbCommand.Parameters.AddRange(dbprams);
                _dbCommand.CommandText = string.Format(_delete, tablename, where);
                res = _dbCommand.ExecuteNonQuery();
            });
            return(res);
        }