What is the difference between ExecuteReader,ExecuteNonQuery
and ExecuteScalar.
Answers were Sorted based on User's Feedback
Answer / rajesh
Execute Reader: it executes the command and returns the result
set of the command
Execute NonQuery: it executes the command and returns the
number of rows affected
Execute Scalar: it executes the command and returns only
first row first column value
Is This Answer Correct ? | 46 Yes | 8 No |
Answer / jerry joseph
ExecuteReader - This method returns a DataReader which is
filled with the data that is retrieved using the command
object.
ExecuteNonQuery - This method returns no data at all. It is
used majorly with Inserts and Updates of tables.
ExecuteScalar - Returns only one value after execution of
the query. It returns the first field in the first row.
This would be excellent for receiving a count of records
(Select Count(*)) in an sql statement, or for any query
where only one specific field in one column is required.
Is This Answer Correct ? | 35 Yes | 9 No |
Answer / surya narayana panda
ExecuteReader-It is readonly forward only.It executes the
command and return the reader objectl.It executes only
select command.
ExecuteNonQuery-It executes command and return's no
value.It returns only how many row effected.It returns
value when the commandType is stroedProcedure with output
parameter.
ExecuteScalar-It executes the command and return a single
value.That is 1st column and 1st row.
Is This Answer Correct ? | 24 Yes | 2 No |
Answer / ashish kumar gupta
Execute Reader: Use for accessing data. It provides a
forward-only, read-only (Select), connected record set.
Execute Non Reader: Use for data manipulation, such as
Insert, Update, and Delete.
Execute Scalar: Use for Retrieving 1 row 1 col. Value. I.e.
single value. E.g. for retrieving aggregate function. It is
faster than other ways of retrieving a single value from
database.
Is This Answer Correct ? | 11 Yes | 0 No |
Answer / sudha
Executereader :-is used with read Method and it will read
the query and fill that in a dataset.we use it in
datareader.
Execute Scalar:-is used when you want a single output.It is
used with Return Value.
ExecuteNonQuery:-This Function is Used When you want to
update your query it does not perform any aoperation like:-
update,deletion,insert.It is used with Perform Action.
Is This Answer Correct ? | 9 Yes | 2 No |
Answer / divya sharma
Execute Non Query- its for manipulation
(insert,delete,update),returns number of rows affected.
Execute Reader- accesses data in read only,forward only way
(perform commands such as select).
Execute scalar- returns only one value i.e. 1st row,1st
column, after execution of query.
Is This Answer Correct ? | 7 Yes | 1 No |
Answer / umarali
ExecuteNonQuery():
1.will work with Action Queries only
(Create,Alter,Drop,Insert,Update,Delete).
2.Retruns the count of rows effected by the Query.
3.Return type is int
4.Return value is optional and can be assigned to an integer
variable.
Example-1 for ExecuteNonQuery Method -Insert:
SqlCommand cmd = new SqlCommand("Insert Into SampleTable
Values('1','2')",con);
//con is the connection object
con.Open();
cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executed
Example-2 for ExecuteNonQuery Method - Update:
public void UpdateEmployeeEmail()
{
SqlConnection conn = new SqlConnection(connString))
String sqlQuery = "UPDATE Employee SET
empemail='umar.ali@xyz.com' WHERE empid=5;
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return count;
}
ExecuterReader():
1.will work with Action and Non-Action Queries (Select)
2.Returns the collection of rows selected by the Query.
3.Return type is DataReader.
4.Return value is compulsory and should be assigned to an
another object DataReader.
Example for ExecuteReader Method:
Here, ExecuteReader is used to get set of records by
specified query, namely, "select * from emp"
SqlConnection con = new SqlConnection(constr); //constructor
can be connection of string.
SqlCommand cmd = new SqlCommand ("select * from emp", con);
con.Open();
SqlDataReader dr = cmd. ExecuteReader (CommandBehavior.
CloseConnection); //Implicitly closes the connection because
CommandBehavior. CloseConnection was specified.
while(dr.Read())
{
Console.WriteLine (dr.GetString(0));
}
dr.Close();
ExecuteScalar():
1.will work with Non-Action Queries that contain aggregate
functions.
2.Return the first row and first column value of the query
result.
3.Return type is object.
4.Return value is compulsory and should be assingned to a
variable of required type.
Example-1 for ExecuteScalar Method:
This returns only one value that is first column value of
the first row in the executed query
public int getSomeProdId()
{
int count=0;
SqlConnection conn = new SqlConnection(connString))
String sqlQuery = "SELECT COUNT(*) FROM dbo.region";
SqlCommand cmd = new SqlCommand(sqlQuery, conn);
try
{
conn.Open();
//Since return type is System.Object, a typecast
is must
count = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return count;
}
Example-2 for ExecuteScalar Method:
This returns one value only, no recordsets.
cmd.CommandText = "Select Name, DOB, from Emp where ID=1";
Dim strName As string = cmd.ExecuteScalar.ToString
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / chitransu verma
ExecuteNonQuery-It executes the DML
commonds(insert,update,delete)and retuns the number of
affected rows.
ExecuteScaler-Lightweight version on executenonquery.
used besically for singalton queries.
It executes the sql statements or stored procedures and
returns a scalar value form first column and first row.
It is used to execute aggregate function like
Avg().Count(*),Max(),Min() etc.When compare to ExecuteReader
It uses Fewer System Resources.
ExecuteReader-It execute sql statement and return the
DataReader Object.
It is used to select records from the tables stored in
database.
Is This Answer Correct ? | 2 Yes | 3 No |
What is Web Services?How we can consume a Web Services in our application?Explain.
What is the difference between a multi-layer and multi-tier applications?
Is there any property names “isnavigating”?
how to do pakaging nd deployment
why should i use FormsAuthentication.RedirectFromLoginPage (); Method in Form Authentication?Without using this method also it's good??
What are the elements of a website?
How may clustered index we can create in table?
What is a pixel url?
What is asp net_sessionid?
Should user input data validation occur server-side or client-side? Why?
3 Answers NIC, Siebel Systems,
How to handle error while project running on live
What are the server control tags in asp.net.?