list 转换成datatable

感谢网上的一位朋友

 1     /// <summary>
 2  60         /// 将集合类转换成DataTable
 3  61         /// </summary>
 4  62         /// <param name="list">集合</param>
 5  63         /// <returns></returns>
 6  64         public static DataTable ToDataTable(IList list)
 7  65         {
 8  66             DataTable result = new DataTable();
 9  67             if (list.Count > 0)
10  68             {
11  69                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
12  70                 foreach (PropertyInfo pi in propertys)
13  71                 {
14  72                     result.Columns.Add(pi.Name, pi.PropertyType);
15  73                 }
16  74
17  75                 for (int i = 0; i < list.Count; i++)
18  76                 {
19  77                     ArrayList tempList = new ArrayList();
20  78                     foreach (PropertyInfo pi in propertys)
21  79                     {
22  80                         object obj = pi.GetValue(list[i], null);
23  81                         tempList.Add(obj);
24  82                     }
25  83                     object[] array = tempList.ToArray();
26  84                     result.LoadDataRow(array, true);
27  85                 }
28  86             }
29  87             return result;
30  88         }

时间: 2024-10-26 19:06:36

list 转换成datatable的相关文章

将list&lt;对象&gt;转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据

领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其他的方法,不过这个方法已经实现,就写一下了: 1.创建表. CREATE TABLE [dbo].[person]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Pwd] [nvarchar](50) NULL, [Age]

C# DataTable转换成实体列表 与 实体列表转换成DataTable

/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T">实体 T </typeparam> /// <param name="table">datatable</param> /// <returns></returns> public static IList<T>

.net excel 转换成datatable,创建文件夹

protected void Button9_Click(object sender, EventArgs e) { string path = ""; path = FileUpload3.PostedFile.FileName; if (path == "") { string jss = "<script language='javascript' type='text/javascript'> alert('先选择文件')</sc

.Net常用技巧_将DataGridView的内容转换成DataTable

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Utility; namespace MyTool { public class GetDgvToT

带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel(转)

步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript"> //导出Excel function exportExcel() { var data = ""; $("#divRptTable").find("table").find("tr").each(function

将泛类型集合List类转换成DataTable

/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list">泛类型集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(List<T> entitys) { //检查实体集合不能为空 if (

讲List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new

C#_List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new

List转DataTable(反射) ; 将泛型集合类转换成DataTable ; 将集合类转换成DataTable

#region 将集合类转换成DataTable /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable ToDataTable(IList list) { DataTable result = new Dat