mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-02-27 16:50:33 +08:00
Update SqlServer BulkCopyUpdate
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user