First: detects the entire entity class.
Using System;
Using System. Collections. Generic;
Using System. Data. sqlclient;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. reflection;
Using System. Data;
Public Partial Class Bangsotest_dr: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Ilist <user> userlist;
String Sqlcmd = " Select Top 1000 username, password from userinfo order by ID " ; // Here, username and password are automatically filled or skipped based on the corresponding fields in the object class.
Using (Sqldatareader DR = bangso. dbutility. sqlhelper. executedatareader (sqlcmd ))
{
Userlist = fillentityfromreader <user> (DR );
}
Foreach(User uInUserlist)
{
Response. Write (U. username +"<Br>");
}
}
Public Static Ilist <t> fillentityfromreader <t> (sqldatareader reader)
{
List <t> List = New List <t> ();
List <propertyinfo> pinfolist = New List <propertyinfo> ();
Boolean checkedpropertyinfo = False ;
While (Reader. Read ())
{
T instance = activator. createinstance <t> ();
# Region Check the matching of database fields and Object Attributes
If (! Checkedpropertyinfo)
{
Foreach (Propertyinfo pinfo In Typeof (T). getproperties ())
{
// Determine whether reader has the boundfieldattribute field in this property.
If (Reader. getschematable (). Select ( " Columnname =' " + Pinfo. Name +" ' " ). Length> 0 )
{
Pinfolist. Add (pinfo );
}
}
}
# Endregion
//Check complete
Checkedpropertyinfo =True;
// Start assignment
Foreach (Propertyinfo infoIn Pinfolist)
{
Int Index = reader. getordinal (info. Name );
If (Reader. getvalue (INDEX )! = Dbnull. value)
{
If (! Info. propertytype. isenum)
{
Info. setvalue (instance, convert. changetype (reader. getvalue (INDEX), info. propertytype ), Null );
}
Else
{
Info. setvalue (instance, enum. parse (info. propertytype, reader. getvalue (INDEX). tostring ()), Null );
}
}
}
list. add (instance);
}< br> reader. close ();
return list;
}
Public ClassUser
{
StringUsername;
Public StringUsername
{
Get{ReturnUsername ;}
Set{Username = value ;}
}
}
}
Second, select the attributes to be filled based on the characteristics in the object class.
Using System;
Using System. Collections. Generic;
Using System. Data. sqlclient;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. reflection;
Public Partial Class Bangsotest_dr: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Ilist <user> userlist;
String Sqlcmd = " Select Top 100 username from userinfo " ;
Using (Sqldatareader DR = bangso. dbutility. sqlhelper. executedatareader (sqlcmd ))
{
Userlist = fillentityfromreader <user> (DR );
}
Foreach(User UIInUserlist)
{
Response. Write (ui. username +"<Br>");
}
}
Public Static Ilist <t> fillentityfromreader <t> (sqldatareader reader)
{
List <t> List = New List <t> ();
List <propertyinfo> pinfolist = New List <propertyinfo> ();
Boolean checkedpropertyinfo = False ;
While (Reader. Read ())
{
T instance = activator. createinstance <t> (); # Region Check the matching of database fields and Object Attributes
If (! Checkedpropertyinfo)
{
Foreach (Propertyinfo pinfo In Typeof (T). getproperties ())
{
Foreach (Boundfieldattribute ATTR In Pinfo. getcustomattributes ( Typeof (Boundfieldattribute ), True ))
{
// Determine whether reader has the boundfieldattribute field in this property.
If (Reader. getschematable (). Select ( " Columnname =' " + ATTR. fieldname + " ' " ). Length> 0 )
{
Pinfolist. Add (pinfo );
}
}
}
}
# Endregion
//Check complete
Checkedpropertyinfo =True;
// Start assignment
Foreach (Propertyinfo info In Pinfolist)
{
Int Index = reader. getordinal (info. Name );
If (Reader. getvalue (INDEX )! = Dbnull. value)
{
If (! Info. propertytype. isenum)
{
Info. setvalue (instance, convert. changetype (reader. getvalue (INDEX), info. propertytype ), Null );
}
Else
{
Info. setvalue (instance, enum. parse (info. propertytype, reader. getvalue (INDEX). tostring ()), Null );
}
}
}
List. Add (instance );
}
ReturnList;
}
Public ClassBoundfieldattribute: attribute
{
Private StringFieldname;
/// <Summary>
/// Name of the bound database Field
/// </Summary>
Public String Fieldname
{
Get { Return Fieldname ;}
}
/// <Summary>
/// Initialize a feature
/// </Summary>
/// <Param name = "_ fieldname"> Database field name </Param>
Public Boundfieldattribute ( String _ Fieldname)
{
This . Fieldname = _ fieldname;
}
}
Public Class User
{
String Username, password;
[Boundfieldattribute ( " Username " )] // The feature declaration here must be required, otherwise it will not be bound
Public String Username
{
Get { Return Username ;}
Set {Username = value ;}
}
Public String Password // The password here will not be filled because it has no declared features
{
Get { Return Password ;}
Set {Password = value ;}
}
}
}