sunkaixuna
3 years ago
9 changed files with 186 additions and 1 deletions
@ -0,0 +1,71 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Data; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public class FastestProvider<T>:IFastest<T> |
|||
{ |
|||
private SqlSugarProvider context; |
|||
private ISugarQueryable<T> queryable; |
|||
|
|||
public FastestProvider(SqlSugarProvider sqlSugarProvider) |
|||
{ |
|||
this.context = sqlSugarProvider; |
|||
this.queryable = this.context.Queryable<T>(); |
|||
} |
|||
public int BulkCopy(List<T> datas) |
|||
{ |
|||
return BulkCopyAsync(datas).GetAwaiter().GetResult(); |
|||
} |
|||
public async Task<int> BulkCopyAsync(List<T> datas) |
|||
{ |
|||
|
|||
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,()=> queryable.Where(it=>false).ToDataTable()); |
|||
var dt = new DataTable(); |
|||
foreach (DataColumn item in tempDataTable.Columns) |
|||
{ |
|||
dt.Columns.Add(item.ColumnName,item.DataType); |
|||
} |
|||
var entityInfo = this.context.EntityMaintenance.GetEntityInfo<T>(); |
|||
dt.TableName =queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName); |
|||
var columns = entityInfo.Columns; |
|||
foreach (var item in datas) |
|||
{ |
|||
var dr = dt.NewRow(); |
|||
foreach (var column in columns) |
|||
{ |
|||
if (column.IsIgnore || column.IsOnlyIgnoreInsert) |
|||
{ |
|||
continue; |
|||
} |
|||
var name = column.DbColumnName; |
|||
if (name == null) |
|||
{ |
|||
name = column.PropertyName; |
|||
} |
|||
var value = ValueConverter(column, PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item)); |
|||
dr[name] = value; |
|||
} |
|||
dt.Rows.Add(dr); |
|||
} |
|||
IFastBuilder buider = new SqlServerFastBuilder(); |
|||
buider.Context = context; |
|||
var result= await buider.ExecuteBulkCopyAsync(dt); |
|||
return result; |
|||
} |
|||
|
|||
private object ValueConverter(EntityColumnInfo columnInfo,object value) |
|||
{ |
|||
if (value == null) |
|||
return value; |
|||
if (value is DateTime&&(DateTime)value == DateTime.MinValue) |
|||
{ |
|||
value = Convert.ToDateTime("1900-01-01"); |
|||
} |
|||
return value; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,16 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Data; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public interface IFastBuilder |
|||
{ |
|||
SqlSugarProvider Context { get; set; } |
|||
|
|||
Task<int> ExecuteBulkCopyAsync(DataTable dt); |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public interface IFastest<T> |
|||
{ |
|||
int BulkCopy(List<T> datas); |
|||
Task<int> BulkCopyAsync(List<T> datas); |
|||
} |
|||
} |
@ -0,0 +1,59 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Data; |
|||
using System.Data.SqlClient; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public class SqlServerFastBuilder: IFastBuilder |
|||
{ |
|||
|
|||
public SqlSugarProvider Context { get; set; } |
|||
|
|||
|
|||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt) |
|||
{ |
|||
|
|||
SqlBulkCopy bulkCopy = GetBulkCopyInstance(); |
|||
bulkCopy.DestinationTableName = dt.TableName; |
|||
try |
|||
{ |
|||
await bulkCopy.WriteToServerAsync(dt); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
CloseDb(); |
|||
throw ex; |
|||
} |
|||
CloseDb(); |
|||
return dt.Rows.Count; |
|||
} |
|||
public SqlBulkCopy GetBulkCopyInstance() |
|||
{ |
|||
SqlBulkCopy copy; |
|||
if (this.Context.Ado.Transaction == null) |
|||
{ |
|||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection); |
|||
} |
|||
else |
|||
{ |
|||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction); |
|||
} |
|||
if (this.Context.Ado.Connection.State == ConnectionState.Closed) |
|||
{ |
|||
this.Context.Ado.Connection.Open(); |
|||
} |
|||
return copy; |
|||
} |
|||
public void CloseDb() |
|||
{ |
|||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null) |
|||
{ |
|||
this.Context.Ado.Connection.Close(); |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue