• DataSet装换为泛型集合方法


     1 /**
     2 *┌──────────────────────────────────────────────────────────────┐
     3 *│ 描    述:数据库相关的工具类                                                   
     4 *│ 作    者:执笔小白-https://www.cnblogs.com/whpepsi/p/3779217.html                                 
     5 *│ 版    本:1.0                                       
     6 *│ 创建时间:2021-10-13 15:40:56                            
     7 *└──────────────────────────────────────────────────────────────┘
     8 *┌──────────────────────────────────────────────────────────────┐
     9 *│ 命名空间: TT                               
    10 *│ 类    名:DataTools                                     
    11 *└──────────────────────────────────────────────────────────────┘
    12 */
    13 using System;
    14 using System.Collections.Generic;
    15 using System.Data;
    16 using System.Reflection;
    17 
    18 namespace WMSTOMESTT
    19 {
    20     public class DataTools
    21     {
    22 
    23         /// <summary>
    24         /// DataSet装换为泛型集合
    25         /// </summary>
    26         /// <typeparam name="T"></typeparam>
    27         /// <param name="p_DataSet">DataSet</param>
    28         /// <param name="p_TableIndex">待转换数据表索引</param>
    29         /// <returns></returns>
    30         public static List<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
    31         {
    32             if (p_DataSet == null || p_DataSet.Tables.Count < 0)
    33                 return null;
    34             if (p_TableIndex > p_DataSet.Tables.Count - 1)
    35                 return null;
    36             if (p_TableIndex < 0)
    37                 p_TableIndex = 0;
    38 
    39             DataTable p_Data = p_DataSet.Tables[p_TableIndex];
    40             // 返回值初始化
    41             List<T> result = new List<T>();
    42             for (int j = 0; j < p_Data.Rows.Count; j++)
    43             {
    44                 T _t = (T)Activator.CreateInstance(typeof(T));
    45                 PropertyInfo[] propertys = _t.GetType().GetProperties();
    46                 foreach (PropertyInfo pi in propertys)
    47                 {
    48                     for (int i = 0; i < p_Data.Columns.Count; i++)
    49                     {
    50                         // 属性与字段名称一致的进行赋值 (枚举类型无法转换,单独拎出来)
    51                         if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
    52                         {
    53                             // 数据库NULL值单独处理
    54                             if (p_Data.Rows[j][i] != DBNull.Value)
    55                                 pi.SetValue(_t, p_Data.Rows[j][i], null);
    56                             else
    57                                 pi.SetValue(_t, null, null);
    58                             break;
    59                         }
    60                     }
    61                 }
    62                 result.Add(_t);
    63             }
    64             return result;
    65         }
    66     }
    67     // 示例
    68     public class DatatoolTest
    69     {
    70         // DataSet装换为泛型集合例子
    71         public void DataSetToIListTest()
    72         {
    73             //List<BilClass> bilClass = DataTools.DataSetToIList<BilClass>(dataSet, 0);
    74         }
    75     }
    76 }

    转自:

    https://www.cnblogs.com/whpepsi/p/3779217.html
    365个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    HTML中select标签单选多选详解
    meta name="viewport" content="width=device-width,initial-scale=1.0" 解释
    鼠标点击变色 lvha
    html 标签释义
    百度首页top设置
    html css 笔记
    iOS 遍历控件
    iOS 状态栏更改为白色
    iOS UIModalPresentationFormSheet风格下的键盘隐藏
    iOS Xcode之SVN(remove git)
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15409700.html
Copyright © 2020-2023  润新知