链接:http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html#2738813 留着学习
using System; using System.Collections.Generic; using System.Collections; using System.Data; namespace CTHFramework { /// <summary> /// DataTable转换成IList /// </summary> /// <typeparam name="T"></typeparam> public class TableToList<T> where T : class, new() { public static IList<T> ToList(DataTable dt, Func<DataRow, T> func) { if (dt == null || dt.Rows.Count == 0) { return null; } IList<T> list = new List<T>(dt.Rows.Count); foreach (DataRow dr in dt.Rows) { list.Add(func(dr)); } return list; } } public class DataItem { public int ID { get; set; } public string Name { get; set; } public override string ToString() { return string.Format("ID:{0},Name:{1}", ID.ToString(), Name); } } static class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID", typeof(int))); dt.Columns.Add(new DataColumn("Name", typeof(string))); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr["ID"] = i.ToString(); dr["Name"] = "Name" + i.ToString(); dt.Rows.Add(dr); } IList<DataItem> list = TableToList<DataItem>.ToList(dt, DataRowToDataItem); foreach (var item in list) { Console.WriteLine(item.ToString()); } Console.Read(); } static DataItem DataRowToDataItem(DataRow dr) { return new DataItem { ID = Convert.ToInt32(dr["ID"]), Name = dr["Name"].ToString() }; } } }
public static void ExInsertBatch<T>(this IDbConnection conn, IEnumerable<T> entityList, string tableName, IDbTransaction transaction = null, bool isEnabled = true) where T : class
{
var tblName = $"dbo.{tableName}";
var tran = (SqlTransaction)transaction;
using (var bulkCopy = new SqlBulkCopy(conn as SqlConnection ?? throw new InvalidOperationException(), SqlBulkCopyOptions.TableLock, tran))
{
var enumerable = entityList as T[] ?? entityList.ToArray();
if (isEnabled)
{
foreach (var item in enumerable)
{
item.SetPropertyMethod();
}
}
bulkCopy.BatchSize = enumerable.ToList().Count;
bulkCopy.DestinationTableName = tblName;
var table = new DataTable();
DapperExtensions.Sql.ISqlGenerator sqlGenerator = new SqlGeneratorImpl(new DapperExtensionsConfiguration());
var classMap = sqlGenerator.Configuration.GetMap<T>();
var props = classMap.Properties.Where(x => x.Ignored == false).ToArray();
foreach (var propertyInfo in props)
{
bulkCopy.ColumnMappings.Add(propertyInfo.Name
, propertyInfo.Name
);
table.Columns.Add(propertyInfo.Name
, Nullable.GetUnderlyingType(propertyInfo.PropertyInfo.PropertyType) ?? propertyInfo.PropertyInfo.PropertyType);
}
var values = new object[props.Count()];
foreach (var itemm in enumerable)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].PropertyInfo.GetValue(itemm, null);
}
table.Rows.Add(values);
}
bulkCopy.WriteToServer(table);
}
}
{
var tblName = $"dbo.{tableName}";
var tran = (SqlTransaction)transaction;
using (var bulkCopy = new SqlBulkCopy(conn as SqlConnection ?? throw new InvalidOperationException(), SqlBulkCopyOptions.TableLock, tran))
{
var enumerable = entityList as T[] ?? entityList.ToArray();
if (isEnabled)
{
foreach (var item in enumerable)
{
item.SetPropertyMethod();
}
}
bulkCopy.BatchSize = enumerable.ToList().Count;
bulkCopy.DestinationTableName = tblName;
var table = new DataTable();
DapperExtensions.Sql.ISqlGenerator sqlGenerator = new SqlGeneratorImpl(new DapperExtensionsConfiguration());
var classMap = sqlGenerator.Configuration.GetMap<T>();
var props = classMap.Properties.Where(x => x.Ignored == false).ToArray();
foreach (var propertyInfo in props)
{
bulkCopy.ColumnMappings.Add(propertyInfo.Name
, propertyInfo.Name
);
table.Columns.Add(propertyInfo.Name
, Nullable.GetUnderlyingType(propertyInfo.PropertyInfo.PropertyType) ?? propertyInfo.PropertyInfo.PropertyType);
}
var values = new object[props.Count()];
foreach (var itemm in enumerable)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].PropertyInfo.GetValue(itemm, null);
}
table.Rows.Add(values);
}
bulkCopy.WriteToServer(table);
}
}