mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-30 22:10:22 +08:00
Add db.Fastest
This commit is contained in:
parent
390563bab2
commit
d0d7ed4a3f
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1175,7 +1175,13 @@ namespace SqlSugar
|
||||
Check.Exception(true,ErrorMessage.GetThrowMessage("Child objects do not support tenant methods, var childDb= Db.GetConnection(confid) ,Db is master ", "Db子对象不支持租户方法,请使用主对象,例如:var childDb= Db.GetConnection(confid) Db是主对象,childDb是子对象 "));
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return new FastestProvider<T>(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
16
Src/Asp.Net/SqlSugar/Interface/IFastBuilder.cs
Normal file
16
Src/Asp.Net/SqlSugar/Interface/IFastBuilder.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
13
Src/Asp.Net/SqlSugar/Interface/IFastest.cs
Normal file
13
Src/Asp.Net/SqlSugar/Interface/IFastest.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -176,5 +176,10 @@ namespace SqlSugar
|
||||
#region Cache
|
||||
SugarCacheProvider DataCache { get; }
|
||||
#endregion
|
||||
|
||||
#region Fastest
|
||||
IFastest<T> Fastest<T>();
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -88,7 +88,11 @@
|
||||
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
|
||||
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
||||
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
||||
<Compile Include="Abstract\FastestProvider\FastestProvider.cs" />
|
||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||
<Compile Include="Interface\IFastBuilder.cs" />
|
||||
<Compile Include="Interface\IFastest.cs" />
|
||||
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" />
|
||||
<Compile Include="SpliteTable\SplitTableAttribute.cs" />
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
|
@ -565,6 +565,13 @@ namespace SqlSugar
|
||||
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return this.Context.Fastest<T>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region More api
|
||||
public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } }
|
||||
public AopProvider Aop => this.Context.Aop;
|
||||
|
@ -641,6 +641,10 @@ namespace SqlSugar
|
||||
return ScopedContext.IsAnyConnection(configId);
|
||||
}
|
||||
|
||||
public IFastest<T> Fastest<T>()
|
||||
{
|
||||
return ScopedContext.Fastest<T>();
|
||||
}
|
||||
private SqlSugarClient GetContext()
|
||||
{
|
||||
SqlSugarClient result = null;
|
||||
|
Loading…
Reference in New Issue
Block a user