Sqldatareader to Object Class 2

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. Data;
Using system. reflection;

Namespace aqioo. modules. Consult. Extensions
{
/// <Summary>
/// Summary description for convertentity1
/// </Summary>
Public static class idatareaderext
{
Public static t readertomodel <t> (this idatareader Dr)
{
// Try
//{
Using (DR)
{
If (dr. Read ())
{
Type modeltype = typeof (t );
Int COUNT = dr. fieldcount;
T model = activator. createinstance <t> ();
For (INT I = 0; I <count; I ++)
{
If (! Isnullordbnull (Dr [I])
{
Propertyinfo Pi = modeltype. getproperty (getpropertyname (dr. getname (I), bindingflags. getproperty | bindingflags. Public | bindingflags. instance | bindingflags. ignorecase );
If (Pi! = NULL)
{
Pi. setvalue (model, hacktype (Dr [I], Pi. propertytype), null );
}
}
}
Return Model;
}
}
Return default (t );
//}
// Catch (exception ex)
//{
// Return default (t );
//}
}

Public static ilist <t> readertolist <t> (this idatareader Dr)
{
Using (DR)
{
List <t> List = new list <t> ();
Type modeltype = typeof (t );
Int COUNT = dr. fieldcount;
While (dr. Read ())
{
T model = activator. createinstance <t> ();

For (INT I = 0; I <count; I ++)
{
If (! Isnullordbnull (Dr [I])
{
Propertyinfo Pi = modeltype. getproperty (getpropertyname (dr. getname (I), bindingflags. getproperty | bindingflags. Public | bindingflags. instance | bindingflags. ignorecase );
If (Pi! = NULL)
{
Pi. setvalue (model, hacktype (Dr [I], Pi. propertytype), null );
}
}
}
List. Add (model );
}
Return list;
}
}
// This class determines whether to convert the null type. Otherwise, an error is returned.
Private Static object hacktype (object value, type conversiontype)
{
If (conversiontype. isgenerictype & conversiontype. getgenerictypedefinition (). Equals (typeof (nullable <> )))
{
If (value = NULL)
Return NULL;

System. componentmodel. nullableconverter = new system. componentmodel. nullableconverter (conversiontype );
Conversiontype = nullableconverter. underlyingtype;
}
Return convert. changetype (value, conversiontype );

}

 

Private Static bool isnullordbnull (Object OBJ)
{
Return (OBJ = NULL | (obj is dbnull ))? True: false;
}

 

// Obtain the bean attribute name corresponding to the DB column

Private Static string getpropertyname (string column)
{

Column = column. tolower ();

String [] narr = column. Split ('_');

Column = "";

For (INT I = 0; I <narr. length; I ++)
{

If (narr [I]. length> 1)
{

Column + = narr [I]. substring (0, 1). toupper () + narr [I]. substring (1 );

}

Else
{

Column + = narr [I]. substring (0, 1). toupper ();

}

}

Return column;

}

}
}

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.