예제 #1
0
 /// <summary>
 /// 查询单个实体
 /// </summary>
 /// <param name="item">框架实体</param>
 /// <returns>单个对象</returns>
 public T SelectModel(FrameWorkItem item)
 {
     try
     {
         var sql = DalAid.CreatePageQuerySql(item.Sql, null, dbType);
         using (var connection = GetDbConnection(item.ConnectionString))
         {
             using (var cmd = GetDbCommand(sql, connection, item.SqlParam))
             {
                 using (IDataReader dr = cmd.ExecuteReader())
                 {
                     if (dr != null && dr.Read())
                     {
                         return(DynamicBuilderEntity <T> .CreateBuilder(dr).Build(dr));
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
     }
     return(default(T));
 }
예제 #2
0
        /// <summary>
        /// 查询集合,带分页
        /// </summary>
        /// <param name="item">框架实体</param>
        /// <param name="isAutoParam">是否自动匹配参数</param>
        /// <param name="dp">分页对象</param>
        /// <returns>对象集合</returns>
        public List <To> SelectList <To>(FrameWorkItem item, DataPage dp = null, bool isAutoParam = false) where To : new()
        {
            try
            {
                var sql   = DalAid.CreatePageQuerySql(item.Sql, dp, dbType);
                var param = item.SqlParam;
                if (isAutoParam)
                {
                    var where = GetWhere(item.SqlParam);
                    sql      += where.Item1;
                    param     = where.Item2;
                }
                using (var connection = GetDbConnection(item.ConnectionString))
                {
                    using (var cmd = GetDbCommand(sql, connection, param))
                    {
                        using (IDataReader dr = cmd.ExecuteReader())
                        {
                            List <To> list = new List <To>();
                            if (dp != null && dp.PageSize > 0)
                            {
                                int result = GetResult <int>(string.Format("SELECT COUNT(1) FROM ({0}) a", item.Sql), item.ConnectionString, item.SqlParam);
                                dp.RowCount = result;
                            }
                            var builders = DynamicBuilderEntity <To> .CreateBuilder(dr);

                            while (dr != null && dr.Read())
                            {
                                To tempT = new To();
                                tempT = builders.Build(dr);
                                list.Add(tempT);
                            }
                            return(list);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }