/// <summary> /// 根据主键获取一行记录 /// </summary> /// <param name="table"></param> /// <param name="key"></param> /// <returns></returns> public CModel GetModelByKey(string table, object key) { CModel m_table = new CModel(table); string sql = SqlSplice.GetSelect(m_table, key); CModel model = null; try { SqlDataReader reader = SqlRequest.GetReader(sql, SqlSplice.SqlParams.ToArray()); while (reader.Read()) { model = new CModel(table); foreach (var field in model.Fields) { model[field] = reader[field]; } } return(model); } catch (Exception ex) { throw new Exception("根据主键查询一行记录出错了", ex); } }
/// <summary> /// 查询多行记录 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List <T> GetModels <T>() { Type type = typeof(T); string sql = SqlSplice.GetSelect(type); List <CModel> models = new List <CModel>(); try { SqlDataReader reader = SqlRequest.GetReader(sql); 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> /// <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); } }