internal async Task ExecuteNonQueryHelperAsync(SqlCommand cmd) { DoPreExecute(cmd); await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); OnExecutedCommand(cmd); }
public virtual async Task NonQueryAsync(MapperDb db, DbCommand command) { var connection = db.GetConnection(); if (connection == null) { throw new MapperException("Could not create database connection."); } await db.OpenSharedConnectionAsync().ConfigureAwait(false); command.Connection = connection; command.Transaction = db.GetTransaction(); await command.ExecuteNonQueryAsync().ConfigureAwait(false); }
public static async ValueTask <int> ExecuteAsync(this System.Data.Common.DbCommand cmd, string command, CancellationToken cancellationToken = default) { cmd.CommandText = command; return(await cmd.ExecuteNonQueryAsync(cancellationToken)); }
private async Task InsertInTransactionAsync(DbCommand sqlCommand, IEnumerable<JournalEntry> journalEntires) { using (var tx = _dbConnection.BeginTransaction()) { sqlCommand.Transaction = tx; try { foreach (var entry in journalEntires) { CopyParamsToCommand(sqlCommand, entry); var commandResult = await sqlCommand.ExecuteNonQueryAsync(); if (commandResult != 1) { //TODO: something went wrong, ExecuteNonQuery() should return 1 (number of rows added) } } tx.Commit(); } catch (Exception) { tx.Rollback(); throw; } } }
private async Task InsertInTransactionAsync(DbCommand sqlCommand, IEnumerable<JournalEntry> journalEntries) { using (var tx = sqlCommand.Connection.BeginTransaction()) { sqlCommand.Transaction = tx; try { foreach (var entry in journalEntries) { CopyParamsToCommand(sqlCommand, entry); var result = await sqlCommand.ExecuteNonQueryAsync(); if (result != 1) { Log.Error("Persisted event operation was expected to return 1, but returned [{0}]", result); } } tx.Commit(); } catch (Exception) { tx.Rollback(); throw; } } }
/// <summary> /// Will execute the given command on the database asynchronously /// </summary> /// <param name="command">The command to execute</param> protected async void ExecuteCommandAsync(DbCommand command) { bool doClose = await InitializeConnectionAsync(); initializeCommand(ref command); await command.ExecuteNonQueryAsync(); if (doClose) Connection.Close(); }
private async Task<int> DoExecuteNonQueryAsync(DbCommand command) { using (DbContext context = await DbContext.GetContextAsync(this.name)) { command.Connection = context.Connection; DoDbEvent(command, DbEventType.BeforeExecution); int rowsAffected = await command.ExecuteNonQueryAsync(); DoDbEvent(command, DbEventType.AfterExecution); return rowsAffected; } }