mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
db.Fastest support BulkCopyUpdate
This commit is contained in:
parent
73181b1ac0
commit
0034f2703a
@ -99,7 +99,8 @@ namespace OrmTest
|
|||||||
|
|
||||||
//Where Sql
|
//Where Sql
|
||||||
//db.Updateable(updateObj).Where("id=@x", new { x = "1" }).ExecuteCommand();
|
//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 ####");
|
Console.WriteLine("#### Updateable End ####");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,8 +109,8 @@ namespace OrmTest
|
|||||||
.AddQueue();
|
.AddQueue();
|
||||||
db.SaveQueues();
|
db.SaveQueues();
|
||||||
|
|
||||||
|
var dataTable = db.Queryable<Order>().Select("id,name,1 as price").Take(2).ToDataTable();
|
||||||
db.Fastest<Order>().BulkCopy(updateObjs);
|
db.Fastest<Order>().BulkUpdate("Order", dataTable,new string[] {"id" },new string[] {"name" });
|
||||||
Console.WriteLine("#### Updateable End ####");
|
Console.WriteLine("#### Updateable End ####");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,28 @@ namespace SqlSugar
|
|||||||
return await _BulkUpdate(datas, whereColumns, updateColumns);
|
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
|
#endregion
|
||||||
|
|
||||||
#region Core
|
#region Core
|
||||||
@ -117,6 +139,28 @@ namespace SqlSugar
|
|||||||
End(datas, false);
|
End(datas, false);
|
||||||
return result;
|
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)
|
private async Task<int> _BulkCopy(List<T> datas)
|
||||||
{
|
{
|
||||||
Begin(datas,true);
|
Begin(datas,true);
|
||||||
|
@ -19,7 +19,8 @@ namespace SqlSugar
|
|||||||
Task<int> BulkUpdateAsync(List<T> datas);
|
Task<int> BulkUpdateAsync(List<T> datas);
|
||||||
int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns);
|
int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns);
|
||||||
Task<int> BulkUpdateAsync(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();
|
SplitFastest<T> SplitTable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user