ExecuteScalar() in SqlCommand Object is used for get a single value from Database after its execution. It executes SQL statements or Stored Procedure and returned a scalar value on first column of first row in the Result Set. If the Result Set contains more than one columns or rows , it takes only the first column of first row, all other values will ignore. If the Result Set is empty it will return a Null reference.It is very useful to use with aggregate functions like Count(*) or Sum() etc. When compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.
It execute Query and return Single value
ExecuteScalar is typically used when your query returns a single value. If it returns more, then the result is the first column of the first row. An example might be SELECT @@IDENTITY AS 'Identity'.
Execute scalar is return only one column and one row from database and this is the one of the ADO.Net Command
If we want to fetch only one value from the back end then we can go for Execute Scalar
The query which returns single result
For fetching a single cell(which rresults from a sql command) value.
ExecuteScalar() It is a predefined member method of Command class. Used to execute and return single record at a time from the database. the return of the ExecuteScalar() method is object
Its always return one value from database after its execution
ExecuteScalar returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
returns Single cell as output. Ex: select max sal from empsingle cell. o/p: 34000
It return first column of first row value
it return first element of first column of first row of table as a object.......
ExecuteScalar is return a single value,the return values type is integer format.when we want table record count only use this executescalar.
It is a command object which is used in DML command and it return single value.
It is a method of command object. It will return only one value, i.e [object] type.
Running MS-SQL Server - Execute Scalar
It is one of the way to execute select command in sqlserver through code behind. It will return first row's first column as a result. Basically use this sqlcommand when we use aggregate function in Sql Query.
Execute Scalar is using to retrieve a data from database
It is used to perform the query which returns singal record like Sum(),Max(),Avrg().It return type will be Object so we need to cast them.
ExecuteScalar is a method of SQLCommand that Executes the query, and returns the first column of the first row of the result set returned by the query. Returns only the first column of the first row. SELECT count(*) FROM table_name;
Execute Scalar is an ADO.NET object which is used to get single value from Back End using Aggregate Functions
In Ado.Net Command Execute Scalar always return a Single Object and you have to cast or convert value from this Object you cant directly use it.
It is one of the command object in ADO.NET. It will return only a single value. That is 1st column of the 1st row value. Ex: All Aggregate functions.
Execute Scalar will work with Non-Action Queries(select) that contain aggregate functions.