Browse Source

Add db.Ado.CancellationToken

pull/1/head
skx 4 years ago
parent
commit
fdcaa7ea3b
  1. 23
      Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs
  2. 4
      Src/Asp.Net/SqlSugar/Interface/IAdo.cs

23
Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs

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

4
Src/Asp.Net/SqlSugar/Interface/IAdo.cs

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

Loading…
Cancel
Save