Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What is the difference between ExecuteReader,ExecuteNonQuery
and ExecuteScalar.

Answers were Sorted based on User's Feedback



What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

What is the difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar...

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

Post New Answer

More ASP.NET Interview Questions

which directive is mandatory in page control

3 Answers   AG Technologies,


Where web.config file is used?

0 Answers   HCL,


Explain Assemblies?,Difference between Panel and GroupBox?,Differences between ASP and ASP.NET?

1 Answers  


Where are session variables stored?

0 Answers  


Can we use multiple forms in single asp.net application?

3 Answers   Polaris,


What is interval time of GC in .net

2 Answers  


what is the role of aspx file ?

2 Answers   Netsweeper,


Asp.Net Source :- In my project i have gridview control - item template. Its have asp:button control. my requirement is while click this button i need to display some alert message. Could you please help me which gridview event i need to write source code? Please give me a quick response. Thanks

6 Answers  


version information of assembly consist of _________ values.

3 Answers   AG Technologies,


How can you execute stored procedure from windows application?

1 Answers   Microsoft,


Can the action attribute of a server-side tag be set to a value and if not how can you possibly pass data from a form page to a subsequent page?

1 Answers  


What definition correctly defines a label server control with the name set to lblHoop? a) <asp:Label name=?lblHoop? runat=?server? /> b) <Label id=?lblHoop? runat=?server? /> c) <asp:Label id=?lblHoop? runat=?server? /> d) <server label name=?lblHoop? runat=?asp? />

3 Answers   Syntax Softtech,


Categories