/// <summary> /// Reader转成T的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="type"></param> /// <param name="dr"></param> /// <param name="fields"></param> /// <param name="isClose"></param> /// <param name="isTry"></param> /// <returns></returns> internal static List <T> DataReaderToList <T>(Type type, IDataReader dr, string fields, bool isClose = true, bool isTry = true) { if (type.Name.Contains("KeyValuePair")) { List <T> strReval = new List <T>(); FillValueTypeToDictionary(type, dr, strReval); return(strReval); } //值类型 else if (type.IsValueType() || type == SqlSugarTool.StringType) { List <T> strReval = new List <T>(); FillValueTypeToDr <T>(type, dr, strReval); return(strReval); } //数组类型 else if (type.IsArray) { List <T> strReval = new List <T>(); FillValueTypeToArray(type, dr, strReval); return(strReval); } var cacheManager = CacheManager <IDataReaderEntityBuilder <T> > .GetInstance(); string key = "DataReaderToList." + fields + type.FullName; IDataReaderEntityBuilder <T> eblist = null; if (cacheManager.ContainsKey(key)) { eblist = cacheManager[key]; } else { eblist = IDataReaderEntityBuilder <T> .CreateBuilder(type, dr); cacheManager.Add(key, eblist, cacheManager.Day); } List <T> list = new List <T>(); try { if (dr == null) { return(list); } while (dr.Read()) { list.Add(eblist.Build(dr)); } if (isClose) { dr.Close(); dr.Dispose(); dr = null; } } catch (Exception ex) { if (isClose) { dr.Close(); dr.Dispose(); dr = null; } Check.Exception(true, "错误信息:实体映射出错。\r\n错误详情:{0}", ex.Message); } return(list); }
/// <summary> /// Reader转成List《T》 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="dr"></param> /// <param name="isClose"></param> /// <returns></returns> internal static List <T> DataReaderToList <T>(Type type, IDataReader dr, string fields, bool isClose = true, bool isTry = true) { if (type.Name.Contains("KeyValuePair")) { List <T> strReval = new List <T>(); FillValueTypeToDictionary(type, dr, strReval); return(strReval); } //值类型 else if (type.IsValueType || type == SqlSugarTool.StringType) { List <T> strReval = new List <T>(); FillValueTypeToDr <T>(type, dr, strReval); return(strReval); } //数组类型 else if (type.IsArray) { List <T> strReval = new List <T>(); FillValueTypeToArray(type, dr, strReval); return(strReval); } var cacheManager = CacheManager <IDataReaderEntityBuilder <T> > .GetInstance(); string key = "DataReaderToList." + fields + type.FullName; IDataReaderEntityBuilder <T> eblist = null; if (cacheManager.ContainsKey(key)) { eblist = cacheManager[key]; } else { eblist = IDataReaderEntityBuilder <T> .CreateBuilder(type, dr); cacheManager.Add(key, eblist, cacheManager.Day); } List <T> list = new List <T>(); try { if (dr == null) { return(list); } while (dr.Read()) { list.Add(eblist.Build(dr)); } if (isClose) { dr.Close(); dr.Dispose(); dr = null; } } catch (Exception ex) { if (isTry)//解决实体变更缓存引起的错误 { try { if (cacheManager.ContainsKey(key)) { //清除实体缓存 cacheManager.Remove(key); return(DataReaderToList <T>(type, dr, fields, isClose, false)); } } catch (Exception innerEx) { throw new Exception("可能实体与数据库类型不匹配,请用 var str = db.ClassGenerating.TableNameToClass(db, \"类名\") 查看正确的实体!!\r\n具体错误信息:" + innerEx.Message); } finally { if (isClose) { dr.Close(); dr.Dispose(); dr = null; } } } else { if (isClose) { dr.Close(); dr.Dispose(); dr = null; } throw ex; } } return(list); }