mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 增加 IInsert.ExecuteSqlBulkCopy 扩展方法执行 SqlBulkCopy 批量插入,在 FreeSql.Provider.SqlServer 可用;
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
public static partial class FreeSqlSqlServerGlobalExtensions
|
||||
{
|
||||
@ -40,6 +41,89 @@ public static partial class FreeSqlSqlServerGlobalExtensions
|
||||
return that;
|
||||
}
|
||||
internal static ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)>();
|
||||
|
||||
/// <summary>
|
||||
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
||||
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
|
||||
/// 使用 WithConnection/WithTransaction 传入连接/事务对象<para></para>
|
||||
/// 提示:若本方法不能满足,请使用 IInsert<T>.ToDataTable 方法得到 DataTable 对象后,自行处理。<para></para>
|
||||
/// SqlCopyBulk 与 insert into t values(..),(..),(..) 性能测试参考:<para></para>
|
||||
/// 插入180000行,52列:21,065ms 与 402,355ms,10列:4,248ms 与 47,204ms<para></para>
|
||||
/// 插入10000行,52列:578ms 与 24,847ms,10列:127ms 与 2,275ms<para></para>
|
||||
/// 插入5000行,52列:326ms 与 11,465ms,10列:71ms 与 1,108ms<para></para>
|
||||
/// 插入2000行,52列:139ms 与 4,971ms,10列:30ms 与 488ms<para></para>
|
||||
/// 插入1000行,52列:105ms 与 2,437ms,10列:48ms 与 279ms<para></para>
|
||||
/// 插入500行,52列:79ms 与 915ms,10列:14ms 与 123ms<para></para>
|
||||
/// 插入100行,52列:60ms 与 138ms,10列:11ms 与 35ms<para></para>
|
||||
/// 插入50行,52列:48ms 与 88ms,10列:10ms 与 16ms<para></para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="copyOptions"></param>
|
||||
/// <param name="batchSize"></param>
|
||||
/// <param name="bulkCopyTimeout"></param>
|
||||
public static void ExecuteSqlBulkCopy<T>(this IInsert<T> that, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, int? batchSize = null, int? bulkCopyTimeout = null) where T : class
|
||||
{
|
||||
var insert = that as FreeSql.SqlServer.Curd.SqlServerInsert<T>;
|
||||
if (insert == null) throw new Exception("ExecuteSqlBulkCopy 是 FreeSql.Provider.SqlServer 特有的功能");
|
||||
|
||||
var dt = that.ToDataTable();
|
||||
if (dt.Rows.Count == 0) return;
|
||||
|
||||
Action<SqlBulkCopy> writeToServer = bulkCopy =>
|
||||
{
|
||||
if (batchSize.HasValue) bulkCopy.BatchSize = batchSize.Value;
|
||||
if (bulkCopyTimeout.HasValue) bulkCopy.BulkCopyTimeout = bulkCopyTimeout.Value;
|
||||
bulkCopy.DestinationTableName = dt.TableName;
|
||||
bulkCopy.WriteToServer(dt);
|
||||
};
|
||||
|
||||
if (insert.InternalConnection == null && insert.InternalTransaction == null)
|
||||
{
|
||||
using (var conn = insert.InternalOrm.Ado.MasterPool.Get())
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(conn.Value as SqlConnection) :
|
||||
new SqlBulkCopy(conn.Value as SqlConnection, copyOptions, null))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (insert.InternalTransaction != null)
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection) :
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection, copyOptions, insert.InternalTransaction))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
}
|
||||
else if (insert.InternalConnection != null)
|
||||
{
|
||||
var conn = insert.InternalConnection;
|
||||
var isNotOpen = false;
|
||||
if (conn.State != System.Data.ConnectionState.Open)
|
||||
{
|
||||
isNotOpen = true;
|
||||
conn.Open();
|
||||
}
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(insert.InternalConnection) :
|
||||
new SqlBulkCopy(insert.InternalConnection, copyOptions, null))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
if (isNotOpen)
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("未实现错误,请反馈给作者");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
|
Reference in New Issue
Block a user