DataReader, DataTable uses generics to fill entity classes

Source: Internet
Author: User

 

Using System; using System. collections. generic; using System. linq; using System. web; // <summary> /// summary of TestTableModel /// </summary> public class TestTableModel {public int D_Id {get; set ;} public string D_Name {get; set;} public string D_Password {get; set;} public string D_Else {get; set;} public decimal D_Amount {get; set ;}}

Using System; using System. collections. generic; using System. linq; using System. web; using System. data; using System. reflection; namespace MSCL {// <summary> // abstract description of ObjectToList /// </summary> public static class ObjectToList {/* -- Example IDataReader dr = MSCL. sqlHelper. getSqlDataReader ("select * from TestTable where d_id in (6, 7, 8)"); List <TestTableModel> t1 = MSCL. objectToList. dataReaderToList <Te StTableModel> (dr); for (int I = 0; I <t1.Count; I ++) {Response. write (t1 [I]. d_Id + "<br/>"); Response. write (t1 [I]. d_Name + "<br/>"); Response. write (t1 [I]. d_Password + "<br/>"); Response. write (t1 [I]. d_Else + "<br/>"); Response. write (t1 [I]. d_Amount + "<br/>");} dr. dispose (); dr. close (); * //// <summary> /// DataReader fills the object class with generics /// </summary> /// <typeparam name = "T"> entity class </ typeparam> // <param name = "Reader"> DataReader </param> // <returns> </returns> public static List <T> DataReaderToList <T> (IDataReader reader) {// instantiate a List <> generic set List <T> DataList = new List <T> (); while (reader. read () {T RowInstance = Activator. createInstance <T> (); // dynamically create a data entity object // obtain all the Property foreach (PropertyInfo Property in typeof (T) of the object through reflection ). getProperties () {try {// obtain the sequence int Ordinal = reader of the current database field. getOrdinal (Property. na Me); if (reader. GetValue (Ordinal )! = DBNull. value) {// fill the data read by DataReader into the Property of the object. setValue (RowInstance, Convert. changeType (reader. getValue (Ordinal), Property. propertyType), null) ;}} catch {break ;}} DataList. add (RowInstance);} return DataList ;} /// <summary> /// DataTable fills the object class with generics /// </summary> /// <typeparam name = "T"> entity class </typeparam> /// <param name = "dt"> DataTable </param> /// <returns> </returns> public static List <T> DataTableToList <T> (DataTable dt) where T: new () {var list = new List <T> (); if (dt = null) return list; var len = dt. rows. count; for (var I = 0; I <len; I ++) {var info = new T (); foreach (DataColumn dc in dt. rows [I]. table. columns) {var field = dc. columnName; var value = dt. rows [I] [field]. toString (); if (string. isNullOrEmpty (value) continue; if (IsDate (value) {value = DateTime. parse (val Ue ). toString ();} var p = info. getType (). getProperty (field); try {if (p. propertyType = typeof (string) {p. setValue (info, value, null);} else if (p. propertyType = typeof (int) {p. setValue (info, int. parse (value), null);} else if (p. propertyType = typeof (bool) {p. setValue (info, bool. parse (value), null);} else if (p. propertyType = typeof (DateTime) {p. setValue (info, DateTime. parse (value ), Null);} else if (p. propertyType = typeof (float) {p. setValue (info, float. parse (value), null);} else if (p. propertyType = typeof (double) {p. setValue (info, double. parse (value), null);} else {p. setValue (info, value, null) ;}} catch (Exception) {// p. setValue (info, ex. message, null) ;}} list. add (info);} dt. dispose (); dt = null; return list;} // <summary> // whether it is time /// </summary> /// <pa Ram name = "d"> </param> // <returns> </returns> private static bool IsDate (string d) {DateTime d1; double d2; return! Double. TryParse (d, out d2) & DateTime. TryParse (d, out d1 );}}}

 

 

Related Article

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.