Add db.Ado.CancellationToken

This commit is contained in:
skx
2020-11-22 12:59:43 +08:00
parent bb842a6dd2
commit fdcaa7ea3b
2 changed files with 23 additions and 4 deletions

View File

@@ -69,6 +69,7 @@ namespace SqlSugar
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnDiffLogEvent;
public virtual List<IDbConnection> SlaveConnections { get; set; }
public virtual IDbConnection MasterConnection { get; set; }
public virtual CancellationToken? CancellationToken { get; set; }
#endregion
#region Connection
@@ -406,7 +407,11 @@ namespace SqlSugar
ExecuteProcessingSQL(ref sql, parameters);
ExecuteBefore(sql, parameters);
var sqlCommand = GetCommand(sql, parameters);
int count = await sqlCommand.ExecuteNonQueryAsync();
int count;
if (this.CancellationToken == null)
count=await sqlCommand.ExecuteNonQueryAsync();
else
count=await sqlCommand.ExecuteNonQueryAsync(this.CancellationToken.Value);
if (this.IsClearParameters)
sqlCommand.Parameters.Clear();
ExecuteAfter(sql, parameters);
@@ -440,7 +445,11 @@ namespace SqlSugar
ExecuteProcessingSQL(ref sql, parameters);
ExecuteBefore(sql, parameters);
var sqlCommand = GetCommand(sql, parameters);
var sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
DbDataReader sqlDataReader;
if(this.CancellationToken==null)
sqlDataReader=await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
else
sqlDataReader=await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default,this.CancellationToken.Value);
if (isSp)
DataReaderParameters = sqlCommand.Parameters;
if (this.IsClearParameters)
@@ -472,7 +481,11 @@ namespace SqlSugar
ExecuteProcessingSQL(ref sql, parameters);
ExecuteBefore(sql, parameters);
var sqlCommand = GetCommand(sql, parameters);
var scalar = await sqlCommand.ExecuteScalarAsync();
object scalar;
if(CancellationToken==null)
scalar=await sqlCommand.ExecuteScalarAsync();
else
scalar = await sqlCommand.ExecuteScalarAsync(this.CancellationToken.Value);
//scalar = (scalar == null ? 0 : scalar);
if (this.IsClearParameters)
sqlCommand.Parameters.Clear();
@@ -1166,6 +1179,10 @@ namespace SqlSugar
#endregion
#region Helper
public virtual void RemoveCancellationToken()
{
this.CancellationToken = null;
}
private void Async()
{
if (this.Context.Root != null & this.Context.Root.AsyncId == null)

View File

@@ -6,6 +6,7 @@ using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SqlSugar
@@ -24,7 +25,7 @@ namespace SqlSugar
StackTraceInfo SqlStackTrace { get; }
IDataParameterCollection DataReaderParameters { get; set; }
CommandType CommandType { get; set; }
CancellationToken? CancellationToken { get; set; }
bool IsDisableMasterSlaveSeparation { get; set; }
bool IsClearParameters { get; set; }
int CommandTimeOut { get; set; }
@@ -157,6 +158,7 @@ namespace SqlSugar
Task<T> SqlQuerySingleAsync<T>(string sql, params SugarParameter[] parameters);
Task<T> SqlQuerySingleAsync<T>(string sql, List<SugarParameter> parameters);
void RemoveCancellationToken();
void Dispose();
void Close();