当前位置:网站首页>EF combined with sqlbulkcopy batch insert data
EF combined with sqlbulkcopy batch insert data
2022-07-24 16:32:00 【Weiwei became a father】
/// <summary>
/// Batch insert data into table
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="connectstring"> string connStr=ConfigurationManager.ConnectionStrings["CodeFirstModel2"].ToString();</param>
/// <param name="tableName"> Table name </param>
/// <param name="list">IList</param>
public void AddByBluckCopy<T>(string connectstring, string tableName, IList<T> list)
{
using (SqlBulkCopy bulk = new SqlBulkCopy(connectstring))
{
bulk.BatchSize = list.Count;
bulk.DestinationTableName = tableName;
var table = new DataTable();
var props = TypeDescriptor.GetProperties(typeof(T)).Cast<PropertyDescriptor>()
.Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System"))
.ToArray();
foreach (var propertyInfo in props)
{
bulk.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
}
var values = new object[props.Length];
foreach (var item in list)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
bulk.WriteToServer(table);
}
}
边栏推荐
- Creation and inheritance of JS class
- Codeforces round 690 (Div. 3) B. last year's substring conventional solution
- Codeforces round 690 (Div. 3) C. unique number conventional solution
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- .NET 测试框架 xUnit,MSTest, Specflow 使用经验汇总
- [Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination
- Enter a URL to this page to show it. What happened in this process?
- 我们为什么要推出Getaverse?
- 如何防止跨站点脚本 (XSS) 攻击完整指南
- [leetcode]75. color classification - problem solving (execution time beat 90%, memory consumption beat 78%)
猜你喜欢

15、ARM嵌入式系统:如何用PC调试单板
![[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination](/img/1d/550a991385b842a21e2b301725407e.png)
[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination

After data management, the quality is still poor

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

thinkphp3.2.5无法跳转到外部链接

如何防止跨站点脚本 (XSS) 攻击完整指南

ArcGIS pixel size changed from 0.00025 to meters

我们为什么要推出Getaverse?

MODIS 16 day data monthly / quarterly synthesis

TCP protocol debugging tool tcpengine v1.3.0 tutorial
随机推荐
Software recommendation - Installation
js,for循环内callback异步转换成同步执行
Jenkins cli command details
Software - prerequisite software
Jenkins CLI 命令详解
QT design robot simulation controller -- key control robot joint rotation
ZCMU--5023: 家庭划分(C语言)
1163: 松哥的分数拆分(C语言)
[technology] online seat selection demo of uniapp
"Heaven and the world, self-respect" -- single case mode
【技术】uniapp 之 在线选座 demo
随笔记:同步、异步和微任务、宏任务的打印顺序
Disassembly of Samsung Galaxy fold: the interior is extremely complex. Is the hinge the main cause of screen damage?
Thinkphp3.2.5 cannot jump to external links
Qt信号和槽连接失败原因及解决办法
Long awaited full platform support - Open Source im project uniapp update of openim
【南京农业大学】考研初试复试资料分享
[leetcode]38. counting - problem solving (execution time beat 91%, memory consumption beat 97%)
leetcode:162. 寻找峰值【二分寻找峰值】
15. ARM embedded system: how to debug single board with PC