Update SqlServer BulkCopyUpdate

This commit is contained in:
sunkaixuan
2022-04-20 17:59:01 +08:00
parent feac4cc923
commit 391fb59fb5
6 changed files with 34 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ namespace SqlSugar
{ {
public class FastBuilder public class FastBuilder
{ {
public virtual bool IsActionUpdateColumns { get; set; }
public SqlSugarProvider Context { get; set; } public SqlSugarProvider Context { get; set; }
public virtual string CharacterSet { get; set; } public virtual string CharacterSet { get; set; }
public virtual string UpdateSql { get; set; } = @"UPDATE TM public virtual string UpdateSql { get; set; } = @"UPDATE TM

View File

@@ -128,6 +128,7 @@ namespace SqlSugar
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
DataTable dt = ToDdateTable(datas); DataTable dt = ToDdateTable(datas);
IFastBuilder buider = GetBuider(); IFastBuilder buider = GetBuider();
ActionIgnoreColums(whereColumns, updateColumns, dt, buider.IsActionUpdateColumns);
buider.Context = context; buider.Context = context;
await buider.CreateTempAsync<T>(dt); await buider.CreateTempAsync<T>(dt);
await buider.ExecuteBulkCopyAsync(dt); await buider.ExecuteBulkCopyAsync(dt);
@@ -142,6 +143,34 @@ namespace SqlSugar
End(datas, false); End(datas, false);
return result; return result;
} }
private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns)
{
if (entityInfo.Columns.Where(it => it.IsIgnore == false).Count() > whereColumns.Length + updateColumns.Length &&IsActionUpdateColumns)
{
var ignoreColums = dt.Columns.Cast<DataColumn>()
.Where(it => !whereColumns.Any(y => y.EqualCase(it.ColumnName)))
.Where(it => !updateColumns.Any(y => y.EqualCase(it.ColumnName))).ToList();
foreach (DataRow item in dt.Rows)
{
foreach (var col in ignoreColums)
{
if (item[col.ColumnName].IsNullOrEmpty())
{
if (col.DataType == UtilConstants.StringType)
{
item[col.ColumnName] = string.Empty;
}
else
{
item[col.ColumnName] = Activator.CreateInstance(col.DataType);
}
}
}
}
}
}
private async Task<int> _BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns) private async Task<int> _BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns)
{ {
var datas = new string[dataTable.Rows.Count].ToList(); var datas = new string[dataTable.Rows.Count].ToList();

View File

@@ -9,6 +9,7 @@ namespace SqlSugar
{ {
public interface IFastBuilder public interface IFastBuilder
{ {
bool IsActionUpdateColumns { get; set; }
SqlSugarProvider Context { get; set; } SqlSugarProvider Context { get; set; }
string CharacterSet { get; set; } string CharacterSet { get; set; }
Task<int> UpdateByTempAsync(string tableName,string tempName,string [] updateColumns,string[] whereColumns); Task<int> UpdateByTempAsync(string tableName,string tempName,string [] updateColumns,string[] whereColumns);

View File

@@ -16,6 +16,7 @@ namespace SqlSugar
} }
public SqlSugarProvider Context { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public SqlSugarProvider Context { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool IsActionUpdateColumns { get; set; }
public void CloseDb() public void CloseDb()
{ {

View File

@@ -11,6 +11,7 @@ namespace SqlSugar
public class SqlServerFastBuilder:FastBuilder,IFastBuilder public class SqlServerFastBuilder:FastBuilder,IFastBuilder
{ {
public override bool IsActionUpdateColumns { get; set; } = true;
public async Task<int> ExecuteBulkCopyAsync(DataTable dt) public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
{ {

View File

@@ -14,6 +14,7 @@ namespace SqlSugar
private bool IsUpdate = false; private bool IsUpdate = false;
public string CharacterSet { get; set; } public string CharacterSet { get; set; }
private DataTable UpdateDataTable { get; set; } private DataTable UpdateDataTable { get; set; }
public bool IsActionUpdateColumns { get; set; }
public SqliteFastBuilder(EntityInfo entityInfo) public SqliteFastBuilder(EntityInfo entityInfo)
{ {
this.entityInfo = entityInfo; this.entityInfo = entityInfo;