"2016-11-2" "persist in Learning" "Day17" "Automatically convert DataReader to entity info by reflection"

Source: Internet
Author: User

After the data from the database is queried by ADO, the DataReader is converted to object info.

  Public classBaseinfo {/// <summary>        ///Fill Entities/// </summary>        /// <param name= "Dr" ></param>         Public Virtual voidFill (DataRow dr) {propertyinfo[] PS= This. GetType ().            GetProperties (); foreach(PropertyInfo pinfoinchPS) {                if(Dr. Table.Columns.Contains (pinfo. Name) {pinfo. SetValue ( This, Dr[pinfo. Name],NULL); }            }        }        /// <summary>        ///Fill Entities/// </summary>        /// <param name= "Dr" ></param>         Public Virtual voidFill (DbDataReader dr) {propertyinfo[] PS= This. GetType ().            GetProperties (); foreach(PropertyInfo pinfoinchPS) {                intColindex =Dr. GetOrdinal (pinfo.                Name); if(Colindex >=0&&!Dr. IsDBNull (Colindex)) {pinfo. SetValue ( This, Dr[pinfo. Name],NULL); }            }        }    }
base class Baseinfo
  Public classBeacondatainfo:baseinfo { Public stringseqno {Get;Set; }  Public stringCBID {Get;Set; }  Public stringtime{Get;Set; }  Public stringdevicename{Get;Set; }  Public stringfirmwaretype{Get;Set; }  Public stringfirmwareversion{Get;Set; }  Public stringlightintensity{Get;Set; }  Public stringmajor{Get;Set; }  PublicDateTime Createtime {Get;Set; }  Public Override voidFill (System.Data.Common.DbDataReader dr) {Base.        Fill (DR); }    }
Object entity class
  PublicList<beaconreceiveinfo>Select () {stringsql ="SELECT * from beaconreceive ORDER BY createtime desc LIMIT 0"; using(DbDataReader dr = Sqlitehelper.executereader (sql, SQLITEHELPER.CONNSTR,NULL, System.Data.CommandType.Text)) {List<BeaconReceiveInfo> LST =NewList<beaconreceiveinfo>();  while(Dr. Read ()) {Beaconreceiveinfo Info=NewBeaconreceiveinfo (); Info.                    Fill (DR); Lst.                ADD (info); }                returnlst; }        }
Business Query Method

"2016-11-2" "persist in Learning" "Day17" "Automatically convert DataReader to entity info by reflection"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.