本文整理汇总了C#中System.Data.Common.DbCommand.ExecuteScalarAsync方法的典型用法代码示例。如果您正苦于以下问题:C# DbCommand.ExecuteScalarAsync方法的具体用法?C# DbCommand.ExecuteScalarAsync怎么用?C# DbCommand.ExecuteScalarAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Common.DbCommand
的用法示例。
在下文中一共展示了DbCommand.ExecuteScalarAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteScalarAsync
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;
}
示例2: DoExecuteScalarAsync
/// <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;
}
}