/// <Summary>
/// Datatable and entity class convert each other
/// </Summary>
/// <Typeparam name = "T"> entity class </typeparam>
Public class modelhandler <t> where T: New ()
{
# Region datatable converted to object class
/// <Summary>
/// Fill the Object List: Fill the object class with the first table of Dataset
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Returns> </returns>
Public list <t> fillmodel (Dataset DS)
{
If (DS = NULL | Ds. Tables [0] = NULL | Ds. Tables [0]. Rows. Count = 0)
{
Return NULL;
}
Else
{
Return fillmodel (Ds. Tables [0]);
}
}
/// <Summary>
/// Fill the Object List: Fill the object class with the index table of Dataset
/// </Summary>
Public list <t> fillmodel (Dataset ds, int index)
{
If (DS = NULL | Ds. Tables. Count <= index | Ds. Tables [Index]. Rows. Count = 0)
{
Return NULL;
}
Else
{
Return fillmodel (Ds. Tables [Index]);
}
}
/// <Summary>
/// Fill the Object List: Fill the object class with datatable
/// </Summary>
Public list <t> fillmodel (datatable DT)
{
If (Dt = NULL | DT. Rows. Count = 0)
{
Return NULL;
}
List <t> modellist = new list <t> ();
Foreach (datarow DR in DT. Rows)
{
// T model = (t) activator. createinstance (typeof (t ));
T model = new T ();
For (INT I = 0; I <dr. Table. Columns. Count; I ++)
{
Propertyinfo = model. GetType (). getproperty (dr. Table. Columns [I]. columnname );
If (propertyinfo! = NULL & Dr [I]! = Dbnull. value)
Propertyinfo. setvalue (model, Dr [I], null );
}
Modellist. Add (model );
}
Return modellist;
}
/// <Summary>
/// Fill object: fill object class with datarow
/// </Summary>
Public t fillmodel (datarow Dr)
{
If (DR = NULL)
{
Return default (t );
}
// T model = (t) activator. createinstance (typeof (t ));
T model = new T ();
For (INT I = 0; I <dr. Table. Columns. Count; I ++)
{
Propertyinfo = model. GetType (). getproperty (dr. Table. Columns [I]. columnname );
If (propertyinfo! = NULL & Dr [I]! = Dbnull. value)
Propertyinfo. setvalue (model, Dr [I], null );
}
Return Model;
}
# Endregion
# Region object class converted to datatable
/// <Summary>
/// Convert the object class to Dataset
/// </Summary>
/// <Param name = "modellist"> object class list </param>
/// <Returns> </returns>
Public dataset filldataset (list <t> modellist)
{
If (modellist = NULL | modellist. Count = 0)
{
Return NULL;
}
Else
{
Dataset DS = new dataset ();
DS. Tables. Add (filldatatable (modellist ));
Return Ds;
}
}
/// <Summary>
/// Convert an object class to a able
/// </Summary>
/// <Param name = "modellist"> object class list </param>
/// <Returns> </returns>
Public datatable filldatatable (list <t> modellist)
{
If (modellist = NULL | modellist. Count = 0)
{
Return NULL;
}
Datatable dt = createdata (modellist [0]);
Foreach (T model in modellist)
{
Datarow = DT. newrow ();
Foreach (propertyinfo in typeof (T). getproperties ())
{
Datarow [propertyinfo. Name] = propertyinfo. getvalue (model, null );
}
DT. Rows. Add (datarow );
}
Return DT;
}
/// <Summary>
/// Obtain the table structure based on the object class
/// </Summary>
/// <Param name = "model"> entity class </param>
/// <Returns> </returns>
Private datatable createdata (T Model)
{
Datatable = new datatable (typeof (T). Name );
Foreach (propertyinfo in typeof (T). getproperties ())
{
Datatable. Columns. Add (New datacolumn (propertyinfo. Name, propertyinfo. propertytype ));
}
Return datatable;
}
# Endregion
}
Datatable and entity classes convert each other