mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-09 02:44:58 +08:00
db.Fastest BulkCopy support datatable
This commit is contained in:
@@ -159,6 +159,10 @@ namespace OrmTest
|
|||||||
|
|
||||||
|
|
||||||
db.Fastest<Order>().BulkCopy(insertObjs);
|
db.Fastest<Order>().BulkCopy(insertObjs);
|
||||||
|
|
||||||
|
|
||||||
|
var dataTable= db.Queryable<Order>().Select("id,name,Price").Take(2).ToDataTable();
|
||||||
|
int result= db.Fastest<Order>().BulkCopy("order", dataTable);
|
||||||
Console.WriteLine("#### Insertable End ####");
|
Console.WriteLine("#### Insertable End ####");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,26 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region BulkCopy
|
#region BulkCopy
|
||||||
|
public int BulkCopy(string tableName,DataTable dt)
|
||||||
|
{
|
||||||
|
return BulkCopyAsync(tableName,dt).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
public async Task<int> BulkCopyAsync(string tableName, DataTable dt)
|
||||||
|
{
|
||||||
|
if (Size > 0)
|
||||||
|
{
|
||||||
|
int resul = 0;
|
||||||
|
await this.context.Utilities.PageEachAsync(dt.Rows.Cast<DataRow>().ToList(), Size, async item =>
|
||||||
|
{
|
||||||
|
resul += await _BulkCopy(tableName,item.CopyToDataTable());
|
||||||
|
});
|
||||||
|
return resul;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await _BulkCopy(tableName,dt);
|
||||||
|
}
|
||||||
|
}
|
||||||
public int BulkCopy(List<T> datas)
|
public int BulkCopy(List<T> datas)
|
||||||
{
|
{
|
||||||
return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
|
return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||||
@@ -107,10 +127,23 @@ namespace SqlSugar
|
|||||||
End(datas,true);
|
End(datas,true);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
private async Task<int> _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
|
#endregion
|
||||||
|
|
||||||
#region AOP
|
#region AOP
|
||||||
private void End(List<T> datas,bool isAdd)
|
private void End<Type>(List<Type> datas,bool isAdd)
|
||||||
{
|
{
|
||||||
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
||||||
this.context.Ado.IsEnableLogEvent = isLog;
|
this.context.Ado.IsEnableLogEvent = isLog;
|
||||||
@@ -120,7 +153,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Begin(List<T> datas,bool isAdd)
|
private void Begin<Type>(List<Type> datas,bool isAdd)
|
||||||
{
|
{
|
||||||
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
||||||
isLog = this.context.Ado.IsEnableLogEvent;
|
isLog = this.context.Ado.IsEnableLogEvent;
|
||||||
|
|||||||
@@ -127,5 +127,39 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
private DataTable GetCopyWriteDataTable(DataTable dt)
|
||||||
|
{
|
||||||
|
DataTable tempDataTable = ReflectionInoCore<DataTable>.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<DataColumn>().Select(it => it.ColumnName.ToLower()).ToList();
|
||||||
|
var columns = dt.Columns.Cast<DataColumn>().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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -11,6 +12,8 @@ namespace SqlSugar
|
|||||||
IFastest<T> PageSize(int Size);
|
IFastest<T> PageSize(int Size);
|
||||||
int BulkCopy(List<T> datas);
|
int BulkCopy(List<T> datas);
|
||||||
Task<int> BulkCopyAsync(List<T> datas);
|
Task<int> BulkCopyAsync(List<T> datas);
|
||||||
|
int BulkCopy(string tableName,DataTable dataTable);
|
||||||
|
Task<int> BulkCopyAsync(string tableName, DataTable dataTable);
|
||||||
|
|
||||||
int BulkUpdate(List<T> datas);
|
int BulkUpdate(List<T> datas);
|
||||||
Task<int> BulkUpdateAsync(List<T> datas);
|
Task<int> BulkUpdateAsync(List<T> datas);
|
||||||
|
|||||||
Reference in New Issue
Block a user