db.Fastest support BulkCopyUpdate

This commit is contained in:
sunkaixuna 2022-01-05 12:17:01 +08:00
parent 73181b1ac0
commit 0034f2703a
4 changed files with 50 additions and 4 deletions

View File

@ -99,7 +99,8 @@ namespace OrmTest
//Where Sql
//db.Updateable(updateObj).Where("id=@x", new { x = "1" }).ExecuteCommand();
var dataTable = db.Queryable<Order>().Select("id,name,1 as price").Take(2).ToDataTable();
db.Fastest<Order>().BulkUpdate("Order", dataTable, new string[] { "id" }, new string[] { "name" });
Console.WriteLine("#### Updateable End ####");
}

View File

@ -109,8 +109,8 @@ namespace OrmTest
.AddQueue();
db.SaveQueues();
db.Fastest<Order>().BulkCopy(updateObjs);
var dataTable = db.Queryable<Order>().Select("id,name,1 as price").Take(2).ToDataTable();
db.Fastest<Order>().BulkUpdate("Order", dataTable,new string[] {"id" },new string[] {"name" });
Console.WriteLine("#### Updateable End ####");
}

View File

@ -94,6 +94,28 @@ namespace SqlSugar
return await _BulkUpdate(datas, whereColumns, updateColumns);
}
}
public int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns)
{
return BulkUpdateAsync(tableName,dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
}
public async Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns)
{
if (Size > 0)
{
int resul = 0;
await this.context.Utilities.PageEachAsync(dataTable.Rows.Cast<DataRow>().ToList(), Size, async item =>
{
resul += await _BulkUpdate(tableName,item.CopyToDataTable(), whereColumns, updateColumns);
});
return resul;
}
else
{
return await _BulkUpdate(tableName,dataTable, whereColumns, updateColumns);
}
}
#endregion
#region Core
@ -117,6 +139,28 @@ namespace SqlSugar
End(datas, false);
return result;
}
private async Task<int> _BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns)
{
var datas = new string[dataTable.Rows.Count].ToList();
Begin(datas, false);
Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0");
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
dataTable.TableName = this.queryable.SqlBuilder.GetTranslationTableName(tableName);
DataTable dt = GetCopyWriteDataTable(dataTable);
IFastBuilder buider = GetBuider();
buider.Context = context;
await buider.CreateTempAsync<object>(dt);
await buider.ExecuteBulkCopyAsync(dt);
//var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
this.context.DbMaintenance.DropTable(dt.TableName);
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
buider.CloseDb();
End(datas, false);
return result;
}
private async Task<int> _BulkCopy(List<T> datas)
{
Begin(datas,true);

View File

@ -19,7 +19,8 @@ namespace SqlSugar
Task<int> BulkUpdateAsync(List<T> datas);
int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns);
Task<int> BulkUpdateAsync(List<T> datas, string[] whereColumns, string[] updateColumns);
int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns);
Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns);
SplitFastest<T> SplitTable();
}
}