DbCommand.ExecuteScalar 方法

定义

当在派生类中重写时,执行查询,并将查询返回的结果集中第一行的第一列返回。When overridden in a derived class, executes the query and returns the first column of the first row in the result set returned by the query. 所有其他的列和行将被忽略。All other columns and rows are ignored.

public:
 abstract System::Object ^ ExecuteScalar();
public abstract object ExecuteScalar ();
public abstract object? ExecuteScalar ();
abstract member ExecuteScalar : unit -> obj
Public MustOverride Function ExecuteScalar () As Object

返回

Object

结果集中第一行的第一列。The first column of the first row in the result set.

实现

注解

使用 ExecuteScalar 方法检索单个值 (例如,从数据库) 聚合值。Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. 这需要比使用方法更少的代码 ExecuteReader ,并使用由返回的数据执行生成单个值所需的操作 DbDataReaderThis requires less code than using the ExecuteReader method and performing the operations necessary to generate the single value using the data returned by a DbDataReader.

如果找不到结果集中第一行的第一列,则 Nothing 返回 Visual Basic) 中 (空引用。If the first column of the first row in the result set is not found, a null reference (Nothing in Visual Basic) is returned. 如果数据库中的值为 null ,则查询返回 DBNull.ValueIf the value in the database is null, the query returns DBNull.Value.

适用于