/// <summary> /// 查询多行记录 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="filter">过滤器</param> /// <returns></returns> public List <T> GetModels <T>(ModelFilter filter) { Type type = typeof(T); string sql = filter.ExecuteSql.ToString(); List <CModel> models = new List <CModel>(); try { SqlDataReader reader = SqlRequest.GetReader(sql, filter.Params.ToArray()); while (reader.Read()) { CModel model = new CModel(type); foreach (var field in model.Fields) { model[field] = reader[field]; } models.Add(model); } reader.Close(); List <T> m_objs = new List <T>(); if (models.Count <= 0) { return(m_objs); } foreach (var model in models) { T m_obj = JsonConverter.CModelToModel <T>(model); m_objs.Add(m_obj); } return(m_objs); } catch (Exception ex) { throw new Exception("查询多行记录出错了", ex); } }
/// <summary> /// 查询一行记录 /// </summary> /// <typeparam name="T"></typeparam> /// <returns>返回一条记录</returns> public T GetModel <T>(ModelFilter filter) { string sql = filter.ExecuteSql.ToString(); CModel model = null; try { SqlDataReader reader = SqlRequest.GetReader(sql, filter.GetParams()); while (reader.Read()) { model = new CModel(filter.TableName); foreach (var field in filter.Fields) { model[field] = reader[field]; } } T m_obj = default(T); reader.Close(); if (model == null) { return(m_obj); } //string str = JsonConverter.SerializeObject(model.FieldsValue); //m_obj = JsonConverter.JsonDeserialize<T>(str); m_obj = JsonConverter.CModelToModel <T>(model); return(m_obj); } catch (Exception ex) { throw new Exception("查询一行数据出错了", ex); } }
/// <summary> /// 根据主键查询一行记录 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T GetModelByKey <T>(object key) { Type type = typeof(T); string sql = SqlSplice.GetSelect(type, key); CModel model = null; try { SqlDataReader reader = SqlRequest.GetReader(sql, SqlSplice.SqlParams.ToArray()); while (reader.Read()) { model = new CModel(type); foreach (var field in model.Fields) { model[field] = reader[field]; } } T m_obj = default(T); reader.Close(); if (model == null) { return(m_obj); } //var t = Activator.CreateInstance(type); m_obj = JsonConverter.CModelToModel <T>(model); return(m_obj); } catch (Exception ex) { throw new Exception("根据主键查询一行记录出错了", ex); } }