• DataTable与DataSet之间的转换Class


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;

    namespace Convert.Common
    {
    public static class DataTableConvert
    {
    /// <summary>
    /// The DataTable type data into a List of <T> entities set t;将DataTable类型的数据转换成List<T>集合 T实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="dataTable"></param>
    /// <returns></returns>
    public static List<T> DataTableToList<T>(DataTable dataTable)
    {
    var list = new List<T>();
    var targetType = typeof (T);
    var allPropertyArray = targetType.GetProperties();
    foreach (DataRow rowElement in dataTable.Rows)
    {
    var element = Activator.CreateInstance<T>();
    foreach (DataColumn columnElement in dataTable.Columns)
    {
    foreach (var property in allPropertyArray)
    {
    if (property.Name.ToUpper().Equals(columnElement.ColumnName.ToUpper()))
    {
    if (rowElement[columnElement.ColumnName] == DBNull.Value ||
    rowElement[columnElement.ColumnName] == "")
    {
    property.SetValue(element, null, null);
    }
    else
    {
    if (property.PropertyType == typeof (string))
    {
    property.SetValue(element, rowElement
    [columnElement.ColumnName].ToString(), null);
    }
    else
    {
    //typeof(Nullable<decimal>)== typeof(decimal?)
    if (property.PropertyType == typeof (decimal?) ||
    property.PropertyType == typeof (decimal))
    {
    property.SetValue(element, decimal.Parse(rowElement
    [columnElement.ColumnName].ToString()), null);
    }
    else if (property.PropertyType == typeof (short?) ||
    property.PropertyType == typeof (short))
    {
    property.SetValue(element, short.Parse(rowElement
    [columnElement.ColumnName].ToString()), null);
    }
    else if (property.PropertyType == typeof (int?) ||
    property.PropertyType == typeof (int))
    {
    property.SetValue(element, int.Parse(rowElement
    [columnElement.ColumnName].ToString()), null);
    }
    else if (property.PropertyType == typeof (DataAction?) ||
    property.PropertyType == typeof (DataAction))
    {
    DataAction? da = null;
    foreach (var name in Enum.GetNames(typeof (DataAction)))
    {
    if (
    !string.IsNullOrEmpty(
    rowElement[columnElement.ColumnName].ToString()))
    {
    if (name == rowElement[columnElement.ColumnName].ToString())
    {
    da =
    (DataAction)
    Enum.Parse(typeof (DataAction),
    rowElement[columnElement.ColumnName]
    .ToString(), true);
    }
    }
    }

    property.SetValue(element, da, null);
    }
    }
    }
    break;
    }
    }
    }

    list.Add(element);
    }
    return list;
    }

    public static DataSet ListToDataSet<T>(List<T> modelList)
    {
    if (modelList == null || modelList.Count == 0)
    {
    return null;
    }
    var ds = new DataSet();
    ds.Tables.Add(ListToDataTable(modelList));
    return ds;
    }

    public static DataTable ListToDataTable<T>(List<T> modelList)
    {
    if (modelList == null || modelList.Count == 0)
    {
    return null;
    }
    var dt = CreateData(modelList[0]);
    foreach (var model in modelList)
    {
    var dataRow = dt.NewRow();
    foreach (var propertyInfo in typeof (T).GetProperties())
    {
    dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
    }
    dt.Rows.Add(dataRow);
    }
    return dt;
    }

    /// <summary>
    /// create DataTable by using model
    /// </summary>
    /// <param name="model">Class entity</param>
    /// <returns></returns>
    private static DataTable CreateData<T>(T model)
    {
    var dataTable = new DataTable(typeof (T).Name);
    foreach (var propertyInfo in typeof (T).GetProperties())
    {
    try
    {
    dataTable.Columns.Add(new DataColumn(propertyInfo.Name.ToUpper(), propertyInfo.PropertyType));
    }
    catch (Exception)
    {
    dataTable.Columns.Add(new DataColumn(propertyInfo.Name.ToUpper(), typeof (string)));
    }
    }
    return dataTable;
    }

    /// <summary>
    /// change position of column in data table
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="columnsName"></param>
    public static void ChangeDataTableColumnPosition(DataTable dt, string columnsName)
    {
    var columnNameArry =
    columnsName.Trim(' ')
    .Trim(' ')
    .ToLower()
    .Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
    .ToList();
    var tbcol = new List<string>();
    var delcol = new List<string>();
    var addcol = new List<string>();
    if (dt != null && dt.Rows.Count > 0 && columnNameArry.Count > 0)
    {
    foreach (DataColumn cl in dt.Columns)
    {
    if (!columnNameArry.Contains(cl.ColumnName.ToLower()))
    {
    delcol.Add(cl.ColumnName.ToLower());
    }
    else
    {
    tbcol.Add(cl.ColumnName.ToLower());
    }
    }
    foreach (var clname in columnNameArry)
    {
    if (!tbcol.Contains(clname))
    {
    dt.Columns.Add(clname, typeof (string));
    }
    }
    foreach (var dclname in delcol)
    {
    dt.Columns.Remove(dclname);
    }
    for (var i = 0; i < columnNameArry.Count; i++)
    {
    dt.Columns[columnNameArry[i]].SetOrdinal(i);
    }
    }
    }

    /// <summary>
    /// change position of column in data table
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="columnsName"></param>
    public static DataTable CreateDataTableColumnsName(string columnsName)
    {
    var dt = new DataTable();
    var columnNameArry =
    columnsName.Trim(' ')
    .Trim(' ')
    .ToLower()
    .Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)
    .ToList();
    foreach (var clname in columnNameArry)
    {
    dt.Columns.Add(clname, typeof (string));
    }
    return dt;
    }
    }
    }

  • 相关阅读:
    设备arduino的编译目录
    c# await 关键字错误
    开篇 hello 内Cool超人
    在Windows Azure虚拟机上开发Windows 8 应用
    使用LVS实现负载平衡之Windows Server 2008配置
    IDC Digital Transition Annual Festival(2018.10.19)
    Dell Technology Summit(2018.10.17)
    Lean Data Innovation Sharing Salon(2018.09.15)
    Trusted Block Chain Summit(2018.10.09)
    Artificial Intelligence Computing Conference(2018.09.12)
  • 原文地址:https://www.cnblogs.com/zunzunQ/p/7580781.html
Copyright © 2020-2023  润新知