From 73181b1ac036d520907699513a401e69ac68d288 Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Wed, 5 Jan 2022 11:22:05 +0800 Subject: [PATCH] db.Fastest BulkCopy support datatable --- .../SqlServerTest/Demo/Demo3_Insertable.cs | 4 ++ .../FastestProvider/FastestProvider.cs | 37 ++++++++++++++++++- .../Abstract/FastestProvider/Private.cs | 34 +++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/IFastest.cs | 3 ++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs index 489042fa7..2121047a8 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs @@ -159,6 +159,10 @@ namespace OrmTest db.Fastest().BulkCopy(insertObjs); + + + var dataTable= db.Queryable().Select("id,name,Price").Take(2).ToDataTable(); + int result= db.Fastest().BulkCopy("order", dataTable); Console.WriteLine("#### Insertable End ####"); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs index be9e61b2b..78f47f280 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/FastestProvider.cs @@ -20,6 +20,26 @@ namespace SqlSugar } #region BulkCopy + public int BulkCopy(string tableName,DataTable dt) + { + return BulkCopyAsync(tableName,dt).ConfigureAwait(true).GetAwaiter().GetResult(); + } + public async Task BulkCopyAsync(string tableName, DataTable dt) + { + if (Size > 0) + { + int resul = 0; + await this.context.Utilities.PageEachAsync(dt.Rows.Cast().ToList(), Size, async item => + { + resul += await _BulkCopy(tableName,item.CopyToDataTable()); + }); + return resul; + } + else + { + return await _BulkCopy(tableName,dt); + } + } public int BulkCopy(List datas) { return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult(); @@ -107,10 +127,23 @@ namespace SqlSugar End(datas,true); return result; } + private async Task _BulkCopy(string tableName,DataTable dataTable) + { + var datas =new string[dataTable.Rows.Count].ToList(); + Begin(datas, true); + DataTable dt = dataTable; + dt.TableName =this.queryable.SqlBuilder.GetTranslationTableName(tableName); + dt = GetCopyWriteDataTable(dt); + IFastBuilder buider = GetBuider(); + buider.Context = context; + var result = await buider.ExecuteBulkCopyAsync(dt); + End(datas, true); + return result; + } #endregion #region AOP - private void End(List datas,bool isAdd) + private void End(List datas,bool isAdd) { var title = isAdd ? "BulkCopy" : "BulkUpdate"; this.context.Ado.IsEnableLogEvent = isLog; @@ -120,7 +153,7 @@ namespace SqlSugar } } - private void Begin(List datas,bool isAdd) + private void Begin(List datas,bool isAdd) { var title = isAdd ? "BulkCopy" : "BulkUpdate"; isLog = this.context.Ado.IsEnableLogEvent; diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs index fd6048980..13a38e467 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs @@ -127,5 +127,39 @@ namespace SqlSugar } return value; } + private DataTable GetCopyWriteDataTable(DataTable dt) + { + DataTable tempDataTable = ReflectionInoCore.GetInstance().GetOrCreate("BulkCopyAsync_dt" + dt.TableName, + () => + { + if (AsName == null) + { + return queryable.Where(it => false).Select("*").ToDataTable(); + } + else + { + return queryable.AS(AsName).Where(it => false).Select("*").ToDataTable(); + } + } + ); + var temColumnsList = tempDataTable.Columns.Cast().Select(it => it.ColumnName.ToLower()).ToList(); + var columns = dt.Columns.Cast().Where(it => temColumnsList.Contains(it.ColumnName.ToLower())).ToList(); + foreach (DataRow item in dt.Rows) + { + DataRow dr = tempDataTable.NewRow(); + foreach (DataColumn column in columns) + { + + dr[column.ColumnName] = item[column.ColumnName]; + if (dr[column.ColumnName] == null) + { + dr[column.ColumnName] = DBNull.Value; + } + } + tempDataTable.Rows.Add(dr); + } + tempDataTable.TableName = dt.TableName; + return tempDataTable; + } } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IFastest.cs b/Src/Asp.Net/SqlSugar/Interface/IFastest.cs index 9d8fb37a6..7f91db84f 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IFastest.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IFastest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Text; using System.Threading.Tasks; @@ -11,6 +12,8 @@ namespace SqlSugar IFastest PageSize(int Size); int BulkCopy(List datas); Task BulkCopyAsync(List datas); + int BulkCopy(string tableName,DataTable dataTable); + Task BulkCopyAsync(string tableName, DataTable dataTable); int BulkUpdate(List datas); Task BulkUpdateAsync(List datas);