首页 > 其他 > 详细

DataSet装换为泛型集合

时间:2018-03-19 10:12:23      阅读:169      评论:0      收藏:0      [点我收藏+]
/// <summary>
/// DataSet装换为泛型集合
/// </summary>
/// <typeparam name="MoDeviceAbnormalStateRecord"></typeparam>
/// <param name="p_DataSet">DataSet</param>
/// <param name="p_TableIndex">待转换数据表索引</param>
/// <returns></returns>
/// 2008-08-01 22:46 HPDV2806
public static List<MoDeviceAbnormalStateRecord> DataSetToIList(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];
        // 返回值初始化 
        List<MoDeviceAbnormalStateRecord> result = new List<MoDeviceAbnormalStateRecord>();
        for (int j = 0; j < p_Data.Rows.Count; j++)
        {
            MoDeviceAbnormalStateRecord _t = (MoDeviceAbnormalStateRecord)Activator.CreateInstance(typeof(MoDeviceAbnormalStateRecord));
            PropertyInfo[] propertys = _t.GetType().GetProperties();
            foreach (PropertyInfo pi in propertys)
            {
                for (int i = 0; i < p_Data.Columns.Count; i++)
                {
                    // 属性与字段名称一致的进行赋值 
                    MoDeviceAbnormalStateRecord Mo = new MoDeviceAbnormalStateRecord();
                    if (pi.Name.Equals(Mo.Datal2MO(p_Data.Columns[i].ColumnName)))
                    {
                        // 数据库NULL值单独处理 
                        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;
    }

DataSet装换为泛型集合

原文:http://blog.51cto.com/11718731/2088363

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!