Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Data. OleDb;
Using System. Reflection;
Namespace PropertyInfo
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Protected object assembleobject (type, system. Data. idatareader reader)
{
Object o = activator. createinstance (type );
If (O = NULL)
Return NULL;
System. reflection. propertyinfo [] properties = type. getproperties (); // declare a variable for retrieving type attributes
For (INT I = 0; I <reader. fieldcount; I ++)
{
System. reflection. propertyinfo property = type. getproperty (reader. getname (I); // obtain all attributes of column I
Object val = reader [I]; // column I
If (Val! = Dbnull. Value & property! = NULL & property. canwrite) // determines whether the object is null, whether the attribute is null, and whether the attribute is writable! True
Property. setvalue (O, reader [I], null); // o object, new value of reader [I] object, blank Index
}
Return O;
}
Public class user
{
Private int _ id;
Public int ID
{
Get {return _ id ;}
Set {_ id = value ;}
}
Private string _ name;
Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Private int _ sex;
Public int sex
{
Get {return _ sex ;}
Set {_ sex = value ;}
}
Private string _ SId;
Public string SId
{
Get {return _ SId ;}
Set {_ SId = value ;}
}
Private string _ Pwd;
Public string Pwd
{
Get {return _ Pwd ;}
Set {_ Pwd = value ;}
}
}
Private void button#click (object sender, EventArgs e)
{
DataTable dt = new DataTable ();
Using (oledbconnection conn = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source = | datadirectory | db2.mdb; persist Security info = true "))
{
String SQL = @ "select * from user ";
Conn. open ();
Oledbcommand cmd = new oledbcommand ("select * From db2.user", Conn );
Oledbdataadapter da = new oledbdataadapter (CMD );
DataSet ds = new DataSet ();
Da. Fill (ds, "dt ");
Dt = ds. Tables [0];
OleDbDataReader dr = cmd. ExecuteReader ();
User u = new User ();
While (dr. Read ())
{
U = (User) AssembleObject (typeof (User), dr );
}
}
}
}
}