Object data type is returned when calling ExecuteScalar method of command object - ADO.NET

Q.  What data type is returned when calling the ExecuteScalar method of a command object?
- Published on 16 Jun 15

a. System.Int32
b. Object
c. No of effected records.
d. None of the above.

ANSWER: Object
 

    Discussion

  • Ramesh   -Posted on 23 Oct 15
    This method returns the single value(The first column of the first row in the result set). The remaining rows and column are ignored. Its return type is Object. If you are using aggregate function in sql, then this method performs very fast comapre to execute reader method.
    Example:
    cmd.CommandText = "SELECT COUNT(*) FROM Employee";
    Int32 count = (Int32) cmd.ExecuteScalar();
    Here cmd is the object of command object.

Post your comment / Share knowledge


Enter the code shown above:
 
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)