Beispiel #1
0
        /// <summary>
        /// 返回指定类型,支持强类型和匿名类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="queryInfo"></param>
        /// <returns></returns>
        internal static List <T> DataReaderToSpecifiedList <T>(DbDataReader reader, QueryInfo <T> queryInfo)
        {
            var mapping = queryInfo.Mapping;
            var list    = new List <T>();

            if (reader.FieldCount == 0)//分页时不会返回查询
            {
                return(list);
            }
            string columnCacheKey = queryInfo.selectKey;
            var    leftColumns    = new Dictionary <string, int>();
            var    dicColumns     = new Dictionary <string, ColumnType>();
            var    a = columnCache.TryGetValue(columnCacheKey, out leftColumns);

            if (!a)
            {
                leftColumns = new Dictionary <string, int>();
                dicColumns  = new Dictionary <string, ColumnType>();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var  name    = reader.GetName(i).ToLower();
                    var  find    = mapping.Find(b => b.ResultName.ToLower() == name);
                    Type proType = typeof(string);
                    if (find == null)
                    {
                        if (!leftColumns.ContainsKey(name))
                        {
                            leftColumns.Add(name, i);
                        }
                    }
                    else
                    {
                        proType = find.PropertyType;
                    }
                    dicColumns.Add(name, new ColumnType()
                    {
                        name = name, index = i, typeName = proType.Name
                    });
                }
                columnCache[columnCacheKey]      = leftColumns;
                queryColumnCache[columnCacheKey] = dicColumns;
            }
            dicColumns = queryColumnCache[columnCacheKey];
            queryInfo.CreateObjCreater(dicColumns);
            var           objCreater      = queryInfo.GetObjCreater();
            int           leftColumnCount = leftColumns.Count;
            var           type            = typeof(T);
            DataContainer dataContainer;

            while (reader.Read())
            {
                dataContainer = new DataContainer(reader, type, dicColumns);
                T detailItem;
                try
                {
                    detailItem = objCreater(dataContainer);
                }
                catch (Exception ero)
                {
                    var    columnType = dataContainer._GetCurrentColumnName();
                    object dataValue  = null;
                    try
                    {
                        dataValue = dataContainer.GetCurrentData();
                    }
                    catch { }
                    reader.Close();
                    reader.Dispose();
                    queryInfo = null;
                    throw new CRLException($"反射赋值时发生错误,在:{type }  字段:{columnType.name} 类型:{columnType.typeName } 值:[{dataValue}],请检查数据库字段类型与对象是否一致 {ero.Message}");
                }
                #region 剩下的放索引
                //按IModel算
                if (leftColumnCount > 0)
                {
                    var model = detailItem as IModel;
                    if (model != null)//当不是IModel,不给索引赋值
                    {
                        foreach (var item in leftColumns)
                        {
                            var col = item.Key;
                            var n   = col.LastIndexOf("__");
                            if (n == -1)
                            {
                                continue;
                            }
                            var mapingName = col.Substring(n + 2);
                            var val        = reader.GetValue(item.Value);
                            model.SetIndexData(mapingName, val);
                        }
                    }
                }
                #endregion
                list.Add(detailItem);
            }
            reader.Close();
            reader.Dispose();
            queryInfo = null;
            return(list);
        }
Beispiel #2
0
        /// <summary>
        /// 返回指定类型,支持强类型和匿名类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="queryInfo"></param>
        /// <returns></returns>
        internal static List <T> DataReaderToSpecifiedList <T>(DbDataReader reader, QueryInfo <T> queryInfo)
        {
            var    mapping        = queryInfo.Mapping;
            var    list           = new List <T>();
            string columnCacheKey = queryInfo.selectKey;
            var    leftColumns    = new Dictionary <string, int>();
            var    dicColumns     = new Dictionary <string, int>();
            var    a = columnCache.TryGetValue(columnCacheKey, out leftColumns);

            if (!a)
            {
                leftColumns = new Dictionary <string, int>();
                dicColumns  = new Dictionary <string, int>();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var name = reader.GetName(i).ToLower();
                    var find = mapping.Count(b => b.QueryName.ToLower() == name);
                    if (find == 0)
                    {
                        leftColumns.Add(name, i);
                    }
                    dicColumns.Add(name, i);
                }
                columnCache[columnCacheKey]      = leftColumns;
                queryColumnCache[columnCacheKey] = dicColumns;
            }
            dicColumns = queryColumnCache[columnCacheKey];
            queryInfo.CreateObjCreater(dicColumns);
            var           objCreater      = queryInfo.GetObjCreater();
            int           leftColumnCount = leftColumns.Count;
            var           type            = typeof(T);
            DataContainer dataContainer;

            while (reader.Read())
            {
                dataContainer = new DataContainer(reader, type, dicColumns);
                T detailItem;
                try
                {
                    detailItem = objCreater(dataContainer);
                }
                catch
                {
                    reader.Close();
                    reader.Dispose();
                    var columnName = dataContainer._GetCurrentColumnName();
                    throw new CRLException("反射赋值时发生错误,在:" + type + " 字段:" + columnName + ",请检查数据库字段类型与对象是否一致");
                }
                #region 剩下的放索引
                //按IModel算
                if (leftColumnCount > 0)
                {
                    var model = detailItem as IModel;
                    foreach (var item in leftColumns)
                    {
                        var col = item.Key;
                        var n   = col.LastIndexOf("__");
                        if (n == -1)
                        {
                            continue;
                        }
                        var mapingName = col.Substring(n + 2);
                        var val        = reader.GetValue(item.Value);
                        model.SetIndexData(mapingName, val);
                    }
                }
                #endregion
                list.Add(detailItem);
            }
            reader.Close();
            reader.Dispose();
            return(list);
        }