ExecuteScalar return value problem

Source: Internet
Author: User

ExecuteScalar return value problem

Dbcommand.executescalar Method:


Executes the query and returns the first column of the first row in the result set returned by the query. All other columns rows will be ignored.

Syntax: Public abstract Object ExecuteScalar ()

Return value: Type: System.Object, the first column of the first row in the result set.

Note: Use the ExecuteScalar method to retrieve a single value (such as an aggregated value) from the database. With the use of ExecuteReader Party
The method then uses the data returned by DbDataReader to perform the actions required to generate a single value, which requires less code than the operation. If you do not find
To the first column of the first row in the result set, a null reference (Nothing in Visual Basic) is returned. If the database in the
Value is NULL, this query returns DBNull.Value.

For querying a single field, do not include the function's SELECT statement, for example, the Select a field from Table A, if no value exists,
The return value of the Dbcommand.executescalar method is null, and the caller needs to be judged using result = = null.


For SELECT statements that use functions, such as select sum (a field) from table A, if no value exists,
The return value of the Dbcommand.exceutescalar method is DBNull.Value, and the caller needs to use result is DBNull or result
= = DBNull.Value to judge.


For the caller of the Dbcommand.executescalar method of ADO, pay attention to the use of the SQL statement used to select the
result = = NULL or use result is DBNull to determine whether the query result is empty. If you use result = = NULL | |
Result is DBNull to determine whether the query result is empty and is more safe to adapt to the two SQL statements, but the efficiency of the operation
will be reduced.

ExecuteScalar return value problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.