コード例 #1
0
 private void FillOutputParameters(DbCommand sourceCommand, CommandParameterCollection parameters)
 {
     if (parameters != null)
     {
         foreach (CommandParameter param in parameters)
         {
             if ((param.Direction == ParameterDirection.Output) || (param.Direction == ParameterDirection.InputOutput))
             {
                 param.Value = sourceCommand.Parameters[param.ParameterName].Value;
             }
         }
     }
 }
コード例 #2
0
ファイル: QueryProvider.cs プロジェクト: Michalau/Kros.KORM
        /// <inheritdoc/>
        public async Task <int> ExecuteNonQueryAsync(
            string query,
            CommandParameterCollection parameters,
            CancellationToken cancellationToken = default)
        {
            CheckCommandParameters(parameters);

            using (OpenConnection())
                using (DbCommand command = CreateCommand(query, parameters))
                {
                    return(await ExecuteNonQueryCommandAsync(command, cancellationToken));
                }
        }
コード例 #3
0
        /// <inheritdoc cref="IQueryProvider.ExecuteStoredProcedure{TResult}(string, CommandParameterCollection)"/>
        public TResult ExecuteStoredProcedure <TResult>(string storedProcedureName, CommandParameterCollection parameters)
        {
            CheckCommandParameters(parameters);

            TResult result = default(TResult);

            Data.ConnectionHelper cnHelper = null;
            DbCommand             command  = null;
            bool callDispose = true;

            try
            {
                cnHelper            = OpenConnection();
                command             = CreateCommand(storedProcedureName, parameters);
                command.CommandType = CommandType.StoredProcedure;
                _logger.LogCommand(command);

                DbParameter returnParameter = GetOrAddCommandReturnParameter(command);

                if (typeof(IEnumerable).IsAssignableFrom(typeof(TResult)))
                {
                    IDataReader reader = new ModelBuilder.QueryDataReader(command, null, cnHelper.CloseConnection);
                    result      = MaterializeStoredProcedureResult <TResult>(reader);
                    callDispose = false;
                }
                else
                {
                    result = MaterializeStoredProcedureResult <TResult>(command, returnParameter);
                }
                FillOutputParameters(command, parameters);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (callDispose)
                {
                    command?.Dispose();
                    cnHelper?.Dispose();
                }
            }

            return(result);
        }
コード例 #4
0
        /// <inheritdoc/>
        public Task <int> ExecuteNonQueryAsync(string query, params object[] paramValues)
        {
            var paramsCollection = new CommandParameterCollection();
            var tempParameters   = new ParamEnumerator(query);

            foreach (var parameter in paramValues)
            {
                if (tempParameters.MoveNext())
                {
                    paramsCollection.Add(tempParameters.Current, parameter);
                }
                else
                {
                    throw new ArgumentException(Resources.MoreParametersThanValues);
                }
            }

            if (tempParameters.MoveNext())
            {
                throw new ArgumentException(Resources.MoreValuesThanParameters);
            }

            return(ExecuteNonQueryAsync(query, paramsCollection));
        }