2016-11-2坚持学习Day17通过反射自动将datareader转为实体info
Posted zscmj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016-11-2坚持学习Day17通过反射自动将datareader转为实体info相关的知识,希望对你有一定的参考价值。
通过ADO.net 查询到数据库的数据后,通过DataReader转为对象Info
public class BaseInfo { /// <summary> /// 填充实体 /// </summary> /// <param name="dr"></param> public virtual void Fill(DataRow dr) { PropertyInfo[] ps = this.GetType().GetProperties(); foreach (PropertyInfo pinfo in ps) { if (dr.Table.Columns.Contains(pinfo.Name)) { pinfo.SetValue(this, dr[pinfo.Name], null); } } } /// <summary> /// 填充实体 /// </summary> /// <param name="dr"></param> public virtual void Fill(DbDataReader dr) { PropertyInfo[] ps = this.GetType().GetProperties(); foreach (PropertyInfo pinfo in ps) { int colIndex = dr.GetOrdinal(pinfo.Name); if (colIndex >= 0 && !dr.IsDBNull(colIndex)) { pinfo.SetValue(this, dr[pinfo.Name], null); } } } }
public class BeaconDataInfo:BaseInfo { public string SeqNO { get; set; } public string CBID { get; set; } public string Time{ get; set; } public string DeviceName{ get; set; } public string FirmwareType{ get; set; } public string FirmwareVersion{ get; set; } public string LightIntensity{ get; set; } public string Major{ get; set; } public DateTime CreateTime { get; set; } public override void Fill(System.Data.Common.DbDataReader dr) { base.Fill(dr); } }
public List<BeaconReceiveInfo> Select() { string sql = "select * from BeaconReceive order by CreateTime desc limit 100 offset 0"; using (DbDataReader dr = SqliteHelper.ExecuteReader(sql, SqliteHelper.ConnStr, null, System.Data.CommandType.Text)) { List<BeaconReceiveInfo> lst = new List<BeaconReceiveInfo>(); while (dr.Read()) { BeaconReceiveInfo info = new BeaconReceiveInfo(); info.Fill(dr); lst.Add(info); } return lst; } }
以上是关于2016-11-2坚持学习Day17通过反射自动将datareader转为实体info的主要内容,如果未能解决你的问题,请参考以下文章