ExecuteScalarAsync() public méthode

public ExecuteScalarAsync ( ) : Task
Résultat Task
        internal async Task <object> ExecuteScalarHelperAsync(SqlCommand cmd)
        {
            DoPreExecute(cmd);
            object r = await cmd.ExecuteScalarAsync().ConfigureAwait(false);

            OnExecutedCommand(cmd);
            return(r);
        }
Exemple #2
0
        protected async Task<object> ExecuteScalarAsync(DbCommand command)
        {
            bool doClose = await InitializeConnectionAsync();
            initializeCommand(ref command);

            object ret = await command.ExecuteScalarAsync();

            if (doClose)
                Connection.Close();

            return ret;
        }
        /// <summary>
        /// 异步执行Command返回单值
        /// </summary>
        /// <param name="command">Command实例</param>
        /// <returns>单值</returns>
        private async Task<object> DoExecuteScalarAsync(DbCommand command)
        {
            ExceptionHelper.TrueThrow<ArgumentException>(command.CommandType == CommandType.TableDirect,
                Resource.ExecuteScalarNotSupportTableDirectException);

            using (DbContext context = await DbContext.GetContextAsync(this.name))
            {
                command.Connection = context.Connection;

                DoDbEvent(command, DbEventType.BeforeExecution);

                object returnValue = await command.ExecuteScalarAsync();

                if (command.CommandType != CommandType.Text)
                {
                    // 由于 SQL Server 在Stored Procedure和Function返回值处理方式上存在不同,因此增加了适应性修改
                    returnValue = (returnValue == null) ? command.Parameters[DefaultReturnValueParameterName].Value : returnValue;
                }

                DoDbEvent(command, DbEventType.AfterExecution);

                return returnValue;
            }
        }