public async Task <int> ExecuteNonQueryAsync(ExecutableQueryExpression query, ParameterBag parameters = null, CancellationToken cancellationToken = default)
 {
     var(sql, autoParameters) = ConvertQuery(query);
     using (var lease = await OpenConnectionAsync(cancellationToken))
         using (var command = CreateCommand(lease.Connection, sql, ParameterBag.Combine(autoParameters, parameters)))
         {
             return(await command.ExecuteNonQueryAsync(cancellationToken));
         }
 }
        public virtual async Task <QueryResult> ExecuteReaderAsync(ExecutableQueryExpression query, ParameterBag parameters = null, CancellationToken cancellationToken = default)
        {
            var(sql, autoParameters) = ConvertQuery(query);
            var lease = await OpenConnectionAsync(cancellationToken);

            var command = CreateCommand(lease.Connection, sql, ParameterBag.Combine(autoParameters, parameters));

            try
            {
                var reader = await command.ExecuteReaderAsync(cancellationToken);

                return(new QueryResult(command, reader, lease));
            }
            catch
            {
                command.Dispose();
                lease.Dispose();
                throw;
            }
        }
        protected virtual DbCommand CreateCommand(DbConnection dbConnection, string sql, ParameterBag parameters)
        {
            var command = dbConnection.CreateCommand();

            command.CommandText = sql;
            if (parameters != null)
            {
                foreach (var kvp in parameters)
                {
                    var parameter = command.CreateParameter();
                    parameter.ParameterName = $"@{kvp.Key}";
                    parameter.Value         = kvp.Value ?? DBNull.Value;
                    command.Parameters.Add(parameter);
                }
            }
            return(command);
        }
Exemple #4
0
        public async Task <QueryResult> ExecuteReaderAsync(ExecutableQueryExpression query, ParameterBag parameters = null, CancellationToken cancellationToken = default)
        {
            var(sql, autoParameters) = _queryProvider.ConvertQuery(query);
            var command = _queryProvider.CreateCommand(_connectionLease.Connection, sql, ParameterBag.Combine(autoParameters, parameters));

            try
            {
                command.Transaction = _transaction;
                var reader = await command.ExecuteReaderAsync();

                return(new QueryResult(command, reader, new RemainOpenConnectionLease(_connectionLease.Connection)));
            }
            catch
            {
                command.Dispose();
                throw;
            }
        }
Exemple #5
0
        public QueryResult ExecuteReader(ExecutableQueryExpression query, ParameterBag parameters = null)
        {
            var(sql, autoParameters) = _queryProvider.ConvertQuery(query);
            var command = _queryProvider.CreateCommand(_connectionLease.Connection, sql, ParameterBag.Combine(autoParameters, parameters));

            try
            {
                command.Transaction = _transaction;
                var reader = command.ExecuteReader();
                return(new QueryResult(command, reader, new RemainOpenConnectionLease(_connectionLease.Connection)));
            }
            catch
            {
                command.Dispose();
                throw;
            }
        }
Exemple #6
0
 public Task <int> ExecuteNonQueryAsync(ExecutableQueryExpression query, ParameterBag parameters = null, CancellationToken cancellationToken = default)
 {
     var(sql, autoParameters) = _queryProvider.ConvertQuery(query);
     using (var command = _queryProvider.CreateCommand(_connectionLease.Connection, sql, ParameterBag.Combine(autoParameters, parameters)))
     {
         command.Transaction = _transaction;
         return(command.ExecuteNonQueryAsync());
     }
 }
Exemple #7
0
 public int ExecuteNonQuery(ExecutableQueryExpression query, ParameterBag parameters = null)
 {
     var(sql, autoParameters) = _queryProvider.ConvertQuery(query);
     using (var command = _queryProvider.CreateCommand(_connectionLease.Connection, sql, ParameterBag.Combine(autoParameters, parameters)))
     {
         command.Transaction = _transaction;
         return(command.ExecuteNonQuery());
     }
 }