Combat. Net data Access Layer-21

Source: Internet
Author: User
Tags static class
Access | data (2) Entityconvert

This dongdong is imitate the product of System.Convert, just don't feel how, use up really a little reluctant to do (the code 1 in the beginning of this article has used this utility)!

Don't believe it, and look at the following code (just two cases J):



Code 16: I change, I change, I change!

public static Class Entityconvert

{

public static IList ToList (DbDataReader rdr, Type entity)

{

ArrayList list = new ArrayList ();

using (RDR)

{

while (RDR. Read ())

{

Object obj = Toobject (RDR, entity, false);

if (obj!= null)

List. ADD (obj);

}

}

return list;

}



public static Object Toobject (DbDataReader rdr,

Type entity, BOOL Bneedread)

{

Object obj = null;

if (!bneedread | | rdr. Read ())

{

Creating a Data Entity object

obj = Activator.CreateInstance (entity);



Set field data for data entity object

fieldinfo[] fields = entity. GetFields (

BindingFlags.Instance | BindingFlags.Public);



foreach (FieldInfo field in fields)

{

String fieldName = field. Name;

Object fieldvalue = Rdr[fieldname];



if (Fieldvalue = = System.DBNull.Value)

Field. SetValue (obj, null);

Else

Field. SetValue (obj, fieldvalue);

}



if (Bneedread &&!rdr. Read ())

Rdr. Close ();

}

return obj;

}

}



















































































































































Next paragraph: http://www.csdn.net/develop/Read_Article.asp?id=27566


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.