mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
-
This commit is contained in:
parent
bdb8a8f1e2
commit
cdcbef2966
@ -444,6 +444,132 @@ namespace SqlSugar
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Core Async
|
||||
public virtual async Task<int> ExecuteCommandAsync(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
int count =await sqlCommand.ExecuteNonQueryAsync();
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
return count;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
if (ErrorEvent != null)
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.IsAutoClose()) this.Close();
|
||||
SetConnectionEnd(sql);
|
||||
}
|
||||
}
|
||||
public virtual async Task<IDataReader> GetDataReaderAsync(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
var isSp = this.CommandType == CommandType.StoredProcedure;
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
var sqlDataReader =await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
|
||||
if (isSp)
|
||||
DataReaderParameters = sqlCommand.Parameters;
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd(sql);
|
||||
return sqlDataReader;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
if (ErrorEvent != null)
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public virtual async Task<IDataReader> GetDataReaderNoCloseAsync(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
var isSp = this.CommandType == CommandType.StoredProcedure;
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
var sqlDataReader = await sqlCommand.ExecuteReaderAsync();
|
||||
if (isSp)
|
||||
DataReaderParameters = sqlCommand.Parameters;
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
SetConnectionEnd(sql);
|
||||
return sqlDataReader;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
if (ErrorEvent != null)
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public virtual async Task<object> GetScalarAsync(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
try
|
||||
{
|
||||
InitParameters(ref sql, parameters);
|
||||
if (FormatSql != null)
|
||||
sql = FormatSql(sql);
|
||||
SetConnectionStart(sql);
|
||||
if (this.ProcessingEventStartingSQL != null)
|
||||
ExecuteProcessingSQL(ref sql, parameters);
|
||||
ExecuteBefore(sql, parameters);
|
||||
var sqlCommand = GetCommand(sql, parameters);
|
||||
var scalar =await sqlCommand.ExecuteScalarAsync();
|
||||
//scalar = (scalar == null ? 0 : scalar);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecuteAfter(sql, parameters);
|
||||
return scalar;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
if (ErrorEvent != null)
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.IsAutoClose()) this.Close();
|
||||
SetConnectionEnd(sql);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public virtual string GetString(string sql, object parameters)
|
||||
|
@ -33,6 +33,10 @@ namespace SqlSugar
|
||||
void SetCommandToAdapter(IDataAdapter adapter, DbCommand command);
|
||||
IDataAdapter GetAdapter();
|
||||
DbCommand GetCommand(string sql, SugarParameter[] parameters);
|
||||
Task<int> ExecuteCommandAsync(string sql, params SugarParameter[] parameters);
|
||||
Task<IDataReader> GetDataReaderAsync(string sql, params SugarParameter[] parameters);
|
||||
Task<IDataReader> GetDataReaderNoCloseAsync(string sql, params SugarParameter[] parameters);
|
||||
Task<object> GetScalarAsync(string sql, params SugarParameter[] parameters);
|
||||
DataTable GetDataTable(string sql, object parameters);
|
||||
DataTable GetDataTable(string sql, params SugarParameter[] parameters);
|
||||
DataTable GetDataTable(string sql, List<SugarParameter> parameters);
|
||||
|
Loading…
Reference in New Issue
Block a user