Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ADO.NET
Can you tell me the return types for execute scalar and execute reader and execute non query
Posted 26-Jun-12 19:16pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

ExecuteNonQuery: Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned.
 
You can use this operation to create database objects or change data in a database by executing UPDATE, INSERT, or DELETE statements. The return value of this operation is of Int32 data type, and:
For the UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the SQL statement.
 
For all other types of statements, the return value is -1.
 
ExecuteReader: Use this operation to execute any arbitrary SQL statements in SQL Server if you want the result set to be returned, if any, as an array of DataSet.
For information about DataSet, see “DataSet Class” at http://go.microsoft.com/fwlink/?LinkId=119631.[^]
(e.g., SELECT col1, col2 from sometable).
 
ExecuteScalar: Use this operation to execute any arbitrary SQL statements in SQL Server to return a single value.
This operation returns the value only in the first column of the first row in the result set returned by the SQL statement.
An example might be SELECT @@IDENTITY AS 'Identity'.
 
Refer similar thread:
What is the difference between Execute Scalar , Execute Reader and ExecuteNonQuery[^]
What is the difference between ExecuteNonQuery, ExecuteReader, and ExecuteScalar?[^]
 
MSDN:
http://msdn.microsoft.com/en-us/library/dd787963(v=bts.10).aspx[^]
http://msdn.microsoft.com/en-us/library/dd788418(v=bts.10).aspx[^]
  Permalink  
Comments
Rahul Rajat Singh at 27-Jun-12 0:36am
   
good answer. +5.
Prasad_Kulkarni at 27-Jun-12 0:38am
   
Thank you Rahul!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

ExecuteNonQuery(): will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).
 
Returns the count of rows effected by the Query.
Return type is int
Return value is optional and can be assigned to an integer variable.
 
ExecuteReader(): will work with Action and Non-Action Queries (Select) Returns the collection of rows selected by the Query.
 
Return type is DataReader.
Return value is compulsory and should be assigned to an another object DataReader.
 
ExecuteScalar(): will work with Non-Action Queries that contain aggregate functions.
 
Return the first row and first column value of the query result.
Return type is object.
Return value is compulsory and should be assigned to a variable of required type
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 3 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100