Dapper.NET 怎么得到 DataTable
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dapper.NET 怎么得到 DataTable相关的知识,希望对你有一定的参考价值。
Dapper.NET conn.Query 方法能转换实体 怎么转换得到 datatable ?
参考技术A 首先 datatable 需要构建 datacolumndatatable dt=new datatable();
dt.columns.add(new datacolumn("列名"))
添加数据方法()
{
datarow dr=dt.newRows();
dr["xxx"]=xxx;
...
dt.Rows.Add(dr);
} 参考技术B Dapper本来就是封装的扩展方法,方便使用的!为何还要DataTable?,如何要获DataTable,你不用Dapper的扩展方法不就行了 参考技术C DataTable table = new DataTable("MyTable");
var reader = conn.ExecuteReader("SELECT * FROM STU");
table.Load(reader);
return table; 参考技术D /// <summary>
/// Converts a Generic List into a DataTable
/// </summary>
/// <param name="list"></param>
/// <param name="typ"></param>
/// <returns></returns>
public static DataTable GetDataTable(IList list, Type typ)
DataTable dt = new DataTable();
// Get a list of all the properties on the object
PropertyInfo[] pi = typ.GetProperties();
// Loop through each property, and add it as a column to the datatable
foreach (PropertyInfo p in pi)
// The the type of the property
Type columnType = p.PropertyType;
// We need to check whether the property is NULLABLE
if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
// If it is NULLABLE, then get the underlying type. eg if "Nullable<int>" then this will return just "int"
columnType = p.PropertyType.GetGenericArguments()[0];
// Add the column definition to the datatable.
dt.Columns.Add(new DataColumn(p.Name, columnType));
// For each object in the list, loop through and add the data to the datatable.
foreach (object obj in list)
object[] row = new object[pi.Length];
int i = 0;
foreach (PropertyInfo p in pi)
row[i++] = p.GetValue(obj, null);
dt.Rows.Add(row);
return dt;
以上是关于Dapper.NET 怎么得到 DataTable的主要内容,如果未能解决你的问题,请参考以下文章