Oracle:EFCore使用OracleBulkCopy实现批量导入数据
Oracle:EFCore使用OracleBulkCopy实现批量导入数据
CSFramework.EF.OracleDatabase
BulkInsert 方法:
C# 全选
public int BulkInsert<T>(List<T> entities, string tableName = null)
{
if (String.IsNullOrEmpty(tableName))
{
var tableAttribute = (TableAttribute)typeof(T).GetCustomAttributes(typeof(TableAttribute), false).First();
tableName = $"\"{tableAttribute.Name}\"";
}
using (OracleBulkCopy bulkCopy = new OracleBulkCopy(ConnectionString))
{
bulkCopy.BulkCopyTimeout = 0;
bulkCopy.BatchSize = entities.Count;
bulkCopy.DestinationTableName = tableName;
bulkCopy.BulkCopyOptions = OracleBulkCopyOptions.UseInternalTransaction | OracleBulkCopyOptions.Default;
var table = DataTableHelper.ToDataTable<T>(entities);
table.TableName = tableName;
//重要:记录设置为新增状态
foreach (DataRow r in table.Rows) r.SetAdded();
bulkCopy.WriteToServer(table);
return table.Rows.Count;
}
}
测试代码
C# 全选
IDatabase db = DatabaseFactory.GetDatabase(GetDatabaseType(), GetConfig("Normal").ConnectionString);
var watch = Stopwatch.StartNew();
var list = new List<_Demo_Customer>();
for (int i = 1; i <= 1000; i++)
{
list.Add(NewCustomer());
}
//调用IDatabase.BulkInsert方法
int x = db.BulkInsert<_Demo_Customer>(list);
watch.Stop();
int count = db.GetQueryable<_Demo_Customer>().Count();
var msg = $"成功:{x},总记录数:{count},用时:{watch.ElapsedMilliseconds}(ms)";
ShowResult("测试BulkInsert方法 -> 结果:" + msg);
测试结果
测试BulkInsert方法 -> 结果:成功:1000,总记录数:8562,用时:115(ms)
----------
测试BulkInsert方法 -> 结果:成功:1000,总记录数:7562,用时:113(ms)
----------
测试BulkInsert方法 -> 结果:成功:1000,总记录数:6562,用时:248(ms)
----------
测试BulkInsert方法 -> 结果:成功:1000,总记录数:5562,用时:134(ms)
----------
测试BulkInsert方法 -> 结果:成功:1000,总记录数:4562,用时:1250(ms)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网