System.Data.Common.DbCommand.ExecuteNonQueryAsync(System.Threading.CancellationToken)

Here are the examples of the csharp api class System.Data.Common.DbCommand.ExecuteNonQueryAsync(System.Threading.CancellationToken) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

39 Examples 7

1. Example

Project: deeply
Source File: ExecuteSqlTask.cs
View license
protected override async Task ExecuteInternalAsync(ITaskContext context)
        {
            using (var connection = this.connectionFactory.CreateConnection())
            {
                connection.Open();
                
                var command = connection.CreateCommand();
                command.CommandText = this.commandText;

                var asyncCommand = command as DbCommand;

                if (asyncCommand == null)
                {
                    // This command doesn't support asynchronous execution
                    // so we will tie up this thread executing normally.
                    // In this instance it is OK because there is no 
                    // message pump that we need to return control to.
                    command.ExecuteNonQuery();
                }
                else
                {
                    await asyncCommand.ExecuteNonQueryAsync(context.CancellationToken);
                }
            }
        }

2. Example

Project: linq2db
Source File: RetryingDbCommand.cs
View license
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
		{
			return _policy.ExecuteAsync(ct => _command.ExecuteNonQueryAsync(ct), cancellationToken);
		}

3. Example

Project: linq2db
Source File: DataConnection.Async.cs
View license
internal async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
		{
			if (TraceSwitch.Level == TraceLevel.Off || OnTraceConnection == null)
				return await ((DbCommand)Command).ExecuteNonQueryAsync(cancellationToken);

			if (TraceSwitch.TraceInfo)
			{
				OnTraceConnection(new TraceInfo(TraceInfoStep.BeforeExecute)
				{
					TraceLevel     = TraceLevel.Info,
					DataConnection = this,
					Command        = Command,
					IsAsync        = true,
				});
			}

			try
			{
				var now = DateTime.Now;
				var ret = await ((DbCommand)Command).ExecuteNonQueryAsync(cancellationToken);

				if (TraceSwitch.TraceInfo)
				{
					OnTraceConnection(new TraceInfo(TraceInfoStep.AfterExecute)
					{
						TraceLevel      = TraceLevel.Info,
						DataConnection  = this,
						Command         = Command,
						ExecutionTime   = DateTime.Now - now,
						RecordsAffected = ret,
						IsAsync         = true,
					});
				}

				return ret;
			}
			catch (Exception ex)
			{
				if (TraceSwitch.TraceError)
				{
					OnTraceConnection(new TraceInfo(TraceInfoStep.Error)
					{
						TraceLevel     = TraceLevel.Error,
						DataConnection = this,
						Command        = Command,
						Exception      = ex,
						IsAsync        = true,
					});
				}

				throw;
			}
		}

4. Example

Project: referencesource
Source File: DBCommand.cs
View license
public Task<int> ExecuteNonQueryAsync() {
            return ExecuteNonQueryAsync(CancellationToken.None);
        }

5. Example

View license
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
        {
            return base.ExecuteNonQueryAsync(cancellationToken);
        }

6. Example

Project: elastic-db-tools
Source File: MockSqlCommand.cs
View license
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
        {
            return base.ExecuteNonQueryAsync(cancellationToken);
        }

7. Example

Project: DistributedLock
Source File: SqlHelpers.cs
View license
private static async Task<int> InternalExecuteNonQueryAndPropagateCancellationAsync(DbCommand command, CancellationToken cancellationToken)
        {
            try
            {
                return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (SqlException ex)
                // MA: canceled SQL operations throw SqlException when canceled instead of OCE.
                // That means that downstream operations end up faulted instead of canceled. We
                // wrap with OCE here to correctly propagate cancellation
                when (cancellationToken.IsCancellationRequested && ex.Number == 0)
            {
                throw new OperationCanceledException(
                    "Command was canceled",
                    ex,
                    cancellationToken
                );
            }
        }

8. Example

Project: nhibernate-core
Source File: Fixture.cs
View license
private static async Task DeleteObjectsOutsideCacheAsync(ISession s, CancellationToken cancellationToken = default(CancellationToken))
		{
			using (var cmd = s.Connection.CreateCommand())
			{
				cmd.CommandText = "DELETE FROM UserTable";
				await (cmd.ExecuteNonQueryAsync(cancellationToken));
			}
		}

9. Example

Project: SmartStoreNET
Source File: CachingCommand.cs
View license
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
		{
			var recordsAffected = await _command.ExecuteNonQueryAsync(cancellationToken);

			InvalidateSetsForNonQuery(recordsAffected);

			return recordsAffected;
		}

10. Example

Project: effort
Source File: DbCommandWrapper.cs
View license
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                var cancelled = new TaskCompletionSource<int>();
                cancelled.SetCanceled();

                return cancelled.Task;
            }

            return this.wrappedCommand.ExecuteNonQueryAsync(cancellationToken);
        }

11. Example

Project: elephant
Source File: DbConnectionExtensions.cs
View license
public static Task<int> ExecuteNonQueryAsync(
            this DbConnection connection,
            string commandText,
            CancellationToken cancellationToken,
            SqlParameter[] sqlParameters = null)
        {
            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.CommandType = System.Data.CommandType.Text;
                if (sqlParameters != null &&
                    sqlParameters.Length > 0)
                {
                    command.Parameters.AddRange(sqlParameters);
                }

                return command.ExecuteNonQueryAsync(cancellationToken);
            }
        }

12. Example

Project: elephant
Source File: SqlSet.cs
View license
public virtual async Task AddAsync(T value)
        {
            if (value == null) throw new Argum/n ..... /n //View Source file for more details /n }

13. Example

View license
public async Task ExecuteNonQueryAsync(SqlNonQueryCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            using (var dbCommand = _dbConnection.CreateCommand())
            {
                dbCommand.Connection = _dbConnection;
                dbCommand.CommandTimeout = _commandTimeout;

                dbCommand.CommandType = command.Type;
                dbCommand.CommandText = command.Text;
                dbCommand.Parameters.AddRange(command.Parameters);
                await dbCommand.ExecuteNonQueryAsync(cancellationToken);
            }
        }

14. Example

View license
public async Task ExecuteNonQueryAsync(SqlNonQueryCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            using (var dbCommand = _dbTransaction.Connection.CreateCommand())
            {
                dbCommand.Connection = _dbTransaction.Connection;
                dbCommand.Transaction = _dbTransaction;
                dbCommand.CommandTimeout = _commandTimeout;

                dbCommand.CommandType = command.Type;
                dbCommand.CommandText = command.Text;
                dbCommand.Parameters.AddRange(command.Parameters);
                await dbCommand.ExecuteNonQueryAsync(cancellationToken);
            }
        }

15. Example

Project: nhibernate-core
Source File: SchemaExport.cs
View license
private async Task ExecuteSqlAsync(DbCommand cmd, string sql, CancellationToken cancellationToken = default(CancellationToken))
		{
			cancellationToken.ThrowIfCancellationRequested();
			if (dialect.SupportsSqlBatches)
			{
				var objFactory = Environment.BytecodeProvider.ObjectsFactory;
				ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql);

				foreach (string stmt in splitter)
				{
					log.DebugFormat("SQL Batch: {0}", stmt);
					cmd.CommandText = stmt;
					cmd.CommandType = CommandType.Text;
					await (cmd.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
				}
			}
			else
			{
				cmd.CommandText = sql;
				cmd.CommandType = CommandType.Text;
				await (cmd.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
			}
		}

16. Example

Project: nhibernate-core
Source File: RefreshFixture.cs
View license
private Task UpdateStatusesAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken))
		{
			try
			{
				var conn = session.Connection;
				var cmd = conn.CreateCommand();
				cmd.CommandText = "UPDATE T_JOB SET JOB_STATUS = 1";
				cmd.CommandType = CommandType.Text;
				session.Transaction.Enlist(cmd);
				return cmd.ExecuteNonQueryAsync(cancellationToken);
			}
			catch (Exception ex)
			{
				return Task.FromException<object>(ex);
			}
		}

17. Example

Project: elephant
Source File: DatabaseSchema.cs
View license
private static async Task AlterTableColumnsAsync(IDatabaseDriver databaseDriver, DbConnection connection, ITable table, IEnumerable<KeyValuePair<string, SqlType>> columns, SqlStatement sqlStatement, CancellationToken cancellationToken)
        {
            foreach (var column in columns)
            {
                var command = connection.CreateCommand();
                command.CommandText = databaseDriver.GetSqlStatementTemplate(
                    sqlStatement).Format(
                        new
                        {
                            schemaName = databaseDriver.ParseIdentifier(table.Schema ?? databaseDriver.DefaultSchema),
                            tableName = databaseDriver.ParseIdentifier(table.Name),
                            columnDefinition = GetColumnsDefinitionSql(databaseDriver, table, new[] { column }).TrimEnd(',')
                        });

                await command.ExecuteNonQueryAsync(cancellationToken);
            }
        }

18. Example

View license
public async Task<int> ExecuteNonQueryAsync(IEnumerable<SqlNonQueryCommand> commands, CancellationToken cancellationToken)
        {
            if (commands == null) 
                throw new ArgumentNullException("commands");

            var count = 0;
            using (var dbCommand = _dbConnection.CreateCommand())
            {
                dbCommand.Connection = _dbConnection;
                dbCommand.CommandTimeout = _commandTimeout;

                foreach (var command in commands)
                {
                    dbCommand.CommandType = command.Type;
                    dbCommand.CommandText = command.Text;
                    dbCommand.Parameters.Clear();
                    dbCommand.Parameters.AddRange(command.Parameters);
                    await dbCommand.ExecuteNonQueryAsync(cancellationToken);
                    count++;
                }
            }
            return count;
        }

19. Example

View license
public async Task<int> ExecuteNonQueryAsync(IEnumerable<SqlNonQueryCommand> commands, CancellationToken cancellationToken)
        {
            if (commands == null) 
                throw new ArgumentNullException("commands");

            var count = 0;
            using (var dbCommand = _dbTransaction.Connection.CreateCommand())
            {
                dbCommand.Connection = _dbTransaction.Connection;
                dbCommand.Transaction = _dbTransaction;
                dbCommand.CommandTimeout = _commandTimeout;

                foreach (var command in commands)
                {
                    dbCommand.CommandType = command.Type;
                    dbCommand.CommandText = command.Text;
                    dbCommand.Parameters.Clear();
                    dbCommand.Parameters.AddRange(command.Parameters);
                    await dbCommand.ExecuteNonQueryAsync(cancellationToken);
                    count++;
                }
            }
            return count;
        }

20. Example

Project: anycmd
Source File: SqlMapperAsync.cs
View license
private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, object param)
        {
            var identity = new Identity(command.CommandText, command.CommandType, cnn, null, param == null ? null : param.GetType(), null);
            var info = GetCacheInfo(identity, param);
            bool wasClosed = cnn.State == ConnectionState.Closed;
            using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
            {
                try
                {
                    if (wasClosed) await ((DbConnection)cnn).OpenAsync().ConfigureAwait(false);
                    return await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
                }
                finally
                {
                    if (wasClosed) cnn.Close();
                }
            }
        }

21. Example

View license
public async Task DoWorkAsync(DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken)
			{
				cancellationToken.ThrowIfCancellationRequested();
				DbCommand stmnt = null;
				try
				{
					stmnt = connection.CreateCommand();
					stmnt.Transaction = transaction;
					stmnt.CommandText = persister.TemporaryIdTableDDL;
					await (stmnt.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
					session.Factory.Settings.SqlStatementLogger.LogCommand(stmnt, FormatStyle.Ddl);
				}
				catch (Exception t)
				{
					log.Debug("unable to create temporary id table [" + t.Message + "]");
				}
				finally
				{
					if (stmnt != null)
					{
						try
						{
							stmnt.Dispose();
						}
						catch (Exception)
						{
							// ignore
						}
					}
				}
			}

22. Example

View license
public async Task DoWorkAsync(DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken)
			{
				cancellationToken.ThrowIfCancellationRequested();
				DbCommand stmnt = null;
				try
				{
					stmnt = connection.CreateCommand();
					stmnt.Transaction = transaction;
					stmnt.CommandText = "drop table " + persister.TemporaryIdTableName;
					await (stmnt.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
					session.Factory.Settings.SqlStatementLogger.LogCommand(stmnt, FormatStyle.Ddl);
				}
				catch (Exception t)
				{
					log.Warn("unable to drop temporary id table after use [" + t.Message + "]");
				}
				finally
				{
					if (stmnt != null)
					{
						try
						{
							stmnt.Dispose();
						}
						catch (Exception)
						{
							// ignore
						}
					}
				}
			}

23. Example

Project: nhibernate-core
Source File: Fixture.cs
View license
private async Task DeleteAllAsync(bool inNHibernateScope, CancellationToken cancellationToken = default(CancellationToken))
		{
			using (ISession session = OpenSession())
			{
				if (inNHibernateScope)
				{
					using (ITransaction trans = session.BeginTransaction())
					{
						await (session.DeleteAsync("from BaseClass", cancellationToken));
						await (trans.CommitAsync(cancellationToken));
					}
				}
				else
				{
					//delete directly from the db
					using (var cmd = session.Connection.CreateCommand())
					{
						cmd.CommandText = "DELETE FROM BaseClass";
						await (cmd.ExecuteNonQueryAsync(cancellationToken));
					}
				}
			}
		}

24. Example

Project: Susanoo
Source File: DatabaseManager.cs
View license
[SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
        public virtual async Task<int> ExecuteNonQueryAsync(string commandText,
            CommandType commandType,
            TimeSpan commandTimeout,
            CancellationToken cancellationToken = default(CancellationToken),
            params DbParameter[] parameters)
        {
            if (string.IsNullOrWhiteSpace(commandText))
                throw new ArgumentNullException(nameof(commandText));

            var open = _explicitlyOpened;

            try
            {
                OpenConnectionInternal();

                using (var command = PrepCommand(Connection, commandText, commandType, commandTimeout, parameters))
                {
                    return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
#if !DOTNETCORE
                if (Transaction.Current == null && !open)
#else
                if (Transaction == null && !open)
#endif
                    CloseConnection();
            }
        }

25. Example

Project: Projac
Source File: SqlCommandExecutor.cs
View license
public async Task ExecuteNonQueryAsync(SqlNonQueryCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            using (var dbConnection = _dbProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = _settings.ConnectionString;
                await dbConnection.OpenAsync(cancellationToken);
                try
                {
                    using (var dbCommand = dbConnection.CreateCommand())
                    {
                        dbCommand.Connection = dbConnection;
                        dbCommand.CommandTimeout = _commandTimeout;
                        dbCommand.CommandType = command.Type;
                        dbCommand.CommandText = command.Text;
                        dbCommand.Parameters.AddRange(command.Parameters);
                        await dbCommand.ExecuteNonQueryAsync(cancellationToken);
                    }
                }
                finally
                {
                    dbConnection.Close();
                }
            }
        }

26. Example

Project: nhibernate-core
Source File: AbstractBatcher.cs
View license
public async Task<int> ExecuteNonQueryAsync(DbCommand cmd, CancellationToken cancellationToken)
		{
			cancellationToken.ThrowIfCancellationRequested();
			await (CheckReadersAsync(cancellationToken)).ConfigureAwait(false);
			LogCommand(cmd);
			await (PrepareAsync(cmd, cancellationToken)).ConfigureAwait(false);
			Stopwatch duration = null;
			if (Log.IsDebugEnabled)
				duration = Stopwatch.StartNew();
			try
			{
				return await (cmd.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
			}
			catch (Exception e)
			{
				e.Data["actual-sql-query"] = cmd.CommandText;
				Log.Error("Could not execute command: " + cmd.CommandText, e);
				throw;
			}
			finally
			{
				if (Log.IsDebugEnabled && duration != null)
					Log.DebugFormat("ExecuteNonQuery took {0} ms", duration.ElapsedMilliseconds);
			}
		}

27. Example

Project: nhibernate-core
Source File: TableGenerator.cs
View license
public override async Task<object> DoWorkInCurrentTransactionAsync(ISessionImplementor session/n ..... /n //View Source file for more details /n }

28. Example

Project: nhibernate-core
Source File: TableStructure.cs
View license
public override async Task<object> DoWorkInCurrentTransactionAsync(ISessionImplementor session/n ..... /n //View Source file for more details /n }

29. Example

Project: nhibernate-core
Source File: SchemaUpdate.cs
View license
public async Task ExecuteAsync(Action<string> scriptAction, bool doUpdate, CancellationToken c/n ..... /n //View Source file for more details /n }

30. Example

Project: elephant
Source File: DatabaseSchema.cs
View license
internal static async Task CreateTableAsync(IDatabaseDriver databaseDriver, DbConnection connection,/n ..... /n //View Source file for more details /n }

31. Example

Project: Projac
Source File: SqlCommandExecutor.cs
View license
public async Task<int> ExecuteNonQueryAsync(IEnumerable<SqlNonQueryCommand> commands, CancellationToken cancellationToken)
        {
            if (commands == null) throw new ArgumentNullException("commands");
            using (var dbConnection = _dbProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = _settings.ConnectionString;
                await dbConnection.OpenAsync(cancellationToken);
                try
                {
                    using (var dbCommand = dbConnection.CreateCommand())
                    {
                        dbCommand.Connection = dbConnection;
                        dbCommand.CommandTimeout = _commandTimeout;
                        var count = 0;
                        foreach (var command in commands)
                        {
                            dbCommand.CommandType = command.Type;
                            dbCommand.CommandText = command.Text;
                            dbCommand.Parameters.Clear();
                            dbCommand.Parameters.AddRange(command.Parameters);
                            await dbCommand.ExecuteNonQueryAsync(cancellationToken);
                            count++;
                        }
                        return count;
                    }
                }
                finally
                {
                    dbConnection.Close();
                }
            }
        }

32. Example

Project: CodeSnippets
Source File: Performance.cs
View license
internal static async Task DbTransactionAsync()
        {
            using (SqlConnection connectio/n ..... /n //View Source file for more details /n }

33. Example

Project: anycmd
Source File: SqlMapperAsync.cs
View license
private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandDefinition comm/n ..... /n //View Source file for more details /n }

34. Example

View license
public async Task ExecuteNonQueryAsync(SqlNonQueryCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            using (var dbConnection = _dbProviderFactory.CreateConnection())
            {
                dbConnection.ConnectionString = _settings.ConnectionString;
                await dbConnection.OpenAsync(cancellationToken);
                try
                {
                    using (var dbTransaction = dbConnection.BeginTransaction(_isolationLevel))
                    {
                        using (var dbCommand = dbConnection.CreateCommand())
                        {
                            dbCommand.Connection = dbConnection;
                            dbCommand.Transaction = dbTransaction;
                            dbCommand.CommandTimeout = _commandTimeout;

                            dbCommand.CommandType = command.Type;
                            dbCommand.CommandText = command.Text;
                            dbCommand.Parameters.AddRange(command.Parameters);
                            await dbCommand.ExecuteNonQueryAsync(cancellationToken);
                        }
                        dbTransaction.Commit();
                    }
                }
                finally
                {
                    dbConnection.Close();
                }
            }
        }

35. Example

Project: CodeSnippets
Source File: Performance.cs
View license
internal static async Task DbTransactionAsync()
        {
            using (SqlConnection connectio/n ..... /n //View Source file for more details /n }

36. Example

Project: CodeSnippets
Source File: Performance.cs
View license
internal static async Task TransactionScopeAsync()
        {
            using (TransactionScope sco/n ..... /n //View Source file for more details /n }

37. Example

View license
public async Task<int> ExecuteNonQueryAsync(IEnumerable<SqlNonQueryCommand> commands, Ca/n ..... /n //View Source file for more details /n }

38. Example

View license
protected override async Task<object> ExecuteAsync(
                    IRelationalConnection /n ..... /n //View Source file for more details /n }

39. Example

Project: nhibernate-core
Source File: TableGenerator.cs
View license
public override async Task<object> DoWorkInCurrentTransactionAsync(ISessionImplementor session/n ..... /n //View Source file for more details /n }