대퍼 대량 삽입
1907 단어 SqlBulkCopydapper
4
/// <summary>
///
/// </summary>
public void InsertBatch<T>(IDbConnection conn, IEnumerable<T> entityList, IDbTransaction transaction = null) where T : class
{
var tblName = string.Format("dbo.{0}", typeof(T).Name);
var tran = (SqlTransaction)transaction;
using (var bulkCopy = new SqlBulkCopy(conn as SqlConnection, SqlBulkCopyOptions.TableLock, tran))
{
bulkCopy.BatchSize = entityList.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 entityList)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].PropertyInfo.GetValue(itemm, null);
}
table.Rows.Add(values);
}
bulkCopy.WriteToServer(table);
}
}
추가 참조:http://www.lanhusoft.com/Article/26.html Dapper Extensions 주소:https://github.com/tmsmith/Dapper-Extensions
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
흔적이 말끔하다.NET 소스 코드 - 소개, 동적 조회업계의 베테랑과 Stack Overflow의 다년간의 홍보를 통해'Dapper with Entity Framework'는 강력한 조합으로 “safe, convenient, efficient, maintainable”...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.