Datatable and entity classes convert each other

Source: Internet
Author: User

/// <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

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.