DbCommand.ExecuteScalar DbCommand.ExecuteScalar DbCommand.ExecuteScalar DbCommand.ExecuteScalar Method


当在派生类中重写时,执行查询,并将查询返回的结果集中第一行的第一列返回。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.

 abstract System::Object ^ ExecuteScalar();
public abstract object ExecuteScalar ();
abstract member ExecuteScalar : unit -> obj
Public MustOverride Function ExecuteScalar () As 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方法更少的代码,并使用DbDataReader由返回的数据执行生成单个值所需的操作。This 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.

如果找不到结果集中第一行的第一列,则返回 null 引用(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.