Mutual conversion between DataSet and generic Sets

Source: Internet
Author: User

Code Description
/// <Summary>
/// The generic set and DataSet convert each other
/// </Summary>
Public class IListDataSet
{
/// <Summary>
/// Set for DataSet
/// </Summary>
/// <Param name = "list"> set </param>
/// <Returns> </returns>
// HPDV2806
Public static DataSet ToDataSet (IList p_List)
{
DataSet result = new DataSet ();
DataTable _ DataTable = new DataTable ();
If (p_List.Count> 0)
{
PropertyInfo [] propertys = p_List [0]. GetType (). GetProperties ();
Foreach (PropertyInfo pi in propertys)
{
_ DataTable. Columns. Add (pi. Name, pi. PropertyType );
}

For (int I = 0; I <p_List.Count; I ++)
{
ArrayList tempList = new ArrayList ();
Foreach (PropertyInfo pi in propertys)
{
Object obj = pi. GetValue (p_List [I], null );
TempList. Add (obj );
}
Object [] array = tempList. ToArray ();
_ DataTable. LoadDataRow (array, true );
}
}
Result. Tables. Add (_ DataTable );
Return result;
}

/// <Summary>
/// Convert a generic set to DataSet
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "list"> generic set </param>
/// <Returns> </returns>
// HPDV2806
Public static DataSet ToDataSet <T> (IList <T> list)
{
Return ToDataSet <T> (list, null );
}

/// <Summary>
/// Convert a generic set to DataSet
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "p_List"> generic set </param>
/// <Param name = "p_PropertyName"> list of property names to be converted </param>
/// <Returns> </returns>
// HPDV2806
Public static DataSet ToDataSet <T> (IList <T> p_List, params string [] p_PropertyName)
{
List <string> propertyNameList = new List <string> ();
If (p_PropertyName! = Null)
PropertyNameList. AddRange (p_PropertyName );

DataSet result = new DataSet ();
DataTable _ DataTable = new DataTable ();
If (p_List.Count> 0)
{
PropertyInfo [] propertys = p_List [0]. GetType (). GetProperties ();
Foreach (PropertyInfo pi in propertys)
{
If (propertyNameList. Count = 0)
{
// If no attribute is specified, all attributes must be converted.
_ DataTable. Columns. Add (pi. Name, pi. PropertyType );
}
Else
{
If (propertyNameList. Contains (pi. Name ))
_ DataTable. Columns. Add (pi. Name, pi. PropertyType );
}
}

For (int I = 0; I <p_List.Count; I ++)
{
ArrayList tempList = new ArrayList ();
Foreach (PropertyInfo pi in propertys)
{
If (propertyNameList. Count = 0)
{
Object obj = pi. GetValue (p_List [I], null );
TempList. Add (obj );
}
Else
{
If (propertyNameList. Contains (pi. Name ))
{
Object obj = pi. GetValue (p_List [I], null );
TempList. Add (obj );
}
}
}
Object [] array = tempList. ToArray ();
_ DataTable. LoadDataRow (array, true );
}
}
Result. Tables. Add (_ DataTable );
Return result;
}

/// <Summary>
/// Replace DataSet with a generic set
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "p_DataSet"> DataSet </param>
/// <Param name = "p_TableIndex"> index of the data table to be converted </param>
/// <Returns> </returns>
// HPDV2806
Public static IList <T> DataSetToIList <T> (DataSet p_DataSet, int p_TableIndex)
{
If (p_DataSet = null | p_DataSet.Tables.Count <0)
Return null;
If (p_TableIndex> p_DataSet.Tables.Count-1)
Return null;
If (p_TableIndex <0)
P_TableIndex = 0;

DataTable p_Data = p_DataSet.Tables [p_TableIndex];
// Return value Initialization
IList <T> result = new List <T> ();
For (int j = 0; j <p_Data.Rows.Count; j ++)
{
T _ t = (T) Activator. CreateInstance (typeof (T ));
PropertyInfo [] propertys = _ t. GetType (). GetProperties ();
Foreach (PropertyInfo pi in propertys)
{
For (int I = 0; I <p_Data.Columns.Count; I ++)
{
// Assign values if the attribute is the same as the field name
If (pi. Name. Equals (p_Data.Columns [I]. ColumnName ))
{
// The NULL Value of the database is processed separately.
If (p_Data.Rows [j] [I]! = DBNull. Value)
Pi. SetValue (_ t, p_Data.Rows [j] [I], null );
Else
Pi. SetValue (_ t, null, null );
Break;
}
}
}
Result. Add (_ t );
}
Return result;
}

/// <Summary>
/// Replace DataSet with a generic set
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "p_DataSet"> DataSet </param>
/// <Param name = "p_TableName"> name of the data table to be converted </param>
/// <Returns> </returns>
// HPDV2806
Public static IList <T> DataSetToIList <T> (DataSet p_DataSet, string p_TableName)
{
Int _ TableIndex = 0;
If (p_DataSet = null | p_DataSet.Tables.Count <0)
Return null;
If (string. IsNullOrEmpty (p_TableName ))
Return null;
For (int I = 0; I <p_DataSet.Tables.Count; I ++)
{
// Obtain the index value of the Table name in the Tables set
If (p_DataSet.Tables [I]. TableName. Equals (p_TableName ))
{
_ TableIndex = I;
Break;
}
}
Return DataSetToIList <T> (p_DataSet, _ TableIndex );
}
}

Scope of use

1. data can be obtained at the business layer. The DataSet can be obtained and converted to the IList set for the caller.

2. Use the custom transmission type in WebServices, that is, the passed parameters use the DataSet type (the data type directly supported by WebServices), and convert it to IList before use.

From: http://www.zxbc.cn/html/20080805/64117.html

Thanks to the original author!

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.