将集合类转换成DataTable

/// <summary>

/// 将集合类转换成DataTale

/// </summary>

/// <param name="list">集合</param>

/// <returns></returns>

public static DataTable ToDataTable(IList list)         {

DataTable result = new DataTable();

if (list == null)

{

return result;

}

if (list.Count > 0)

{

PropertyInfo[] propertys = list[0].GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

result.Columns.Add(pi.Name, pi.PropertyType);

}

for (int i = 0; i < list.Count; i++)

{

ArrayList tempList = new ArrayList();

foreach (PropertyInfo pi in propertys)

{

object obj = pi.GetValue(list[i], null);

tempList.Add(obj);

}

object[] array = tempList.ToArray();

result.LoadDataRow(array, true);

}

}

return result;

}

时间: 2024-10-12 07:49:48

将集合类转换成DataTable的相关文章

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

将List集合类转换成DataTable

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

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 Data

将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 (