IT Community - Software Programming, Web Development and Technical Support

difference between ExecuteQuery And Execute NonQuery

This is a discussion on difference between ExecuteQuery And Execute NonQuery within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi, What is the difference between ExecuteQuery And Execute NonQuery?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-18-2008, 09:09 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default difference between ExecuteQuery And Execute NonQuery

Hi,
What is the difference between ExecuteQuery And Execute NonQuery?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-18-2008, 09:09 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

executeQuery() is for command objects i.e., this method will send "select statement" to database and returns value given by the database.



executeNonQuery() uses the "Insert/Update/Delete/Stored subprogram" , this method will return number of records effected by the database table
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-18-2008, 09:34 PM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Cool Re: difference between ExecuteQuery And Execute NonQuery

While surfing through net i got the information such as

"ExecuteQuery is the method which is used to return the result of the command like select Query. The Execute Non Query is used to return the Query as the statement like the update Delete Insert that returns No data."


But there is no method named ExecuteQuery in Ado.net.There are four methods which can be applied on command objects, ExecureReader, ExecuteNonQuery, ExecuteScalar, and ExecuteXmlReader.
If you have any proof about the exixtence of the method ExecuteQuery then sent it ti me.

Even to select some data from the DB we don't need to use the ExecuteQuery or ExecuteNonQuery(). Only to updat,insert or delete data we are using ExecuteNonQuery().

Last edited by Kirubhananth : 03-24-2008 at 04:42 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 04:14 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: difference between ExecuteQuery And Execute NonQuery

ExecuteReader expects to run a query command or a stored procedure that selects records. It expects to have one or more resultsets to return.
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
// process the resultset(s) here
cmd.Connection.Close();

You access the selected records using the SqlDataReader object and use the method Read to loop through them. You move to the next resultset using the NextResults method.

ExecuteNonQuery
expects to run a command, or a stored procedure, that affects the state of the specified table. This means anything but a query command. You normally use this method to issue an INSERT, UPDATE, DELETE, CREATE, and SET statement.

ExecuteNonQuery returns only the number of rows affected by the command execution, or –1 should this information be unavailable. It doesn't give you a chance to access any result set generated by the statement or the stored procedure. Actually, there's really nothing to prevent you from using this method for a query command, but in this case you get neither the resultset nor the number of the affected rows.
cmd.Connection.Open();
nRecsAffected = cmd.ExecuteNonQuery();
cmd.Connection.Close();
// check the record(s) affected here

The number of affected rows is also made available through the RecordsAffected property of the SqlCommand object. This property equals –1 in case of errors or if a query command is executed.

ExecuteScalar
expects to run a query command, or more likely a stored procedure, that returns data. However, this method is different from ExecuteReader in that it just makes available, as a scalar value, the first column on the first row of the selected resultset.
cmd.Connection.Open();
Object o = cmd.ExecuteScalar(); cmd.Connection.Close();
// work on the scalar here

The method returns the value as a boxed object. It's then up to you to unbox or cast that value to the proper, expected type.

ExecuteScalar turns out to be particularly useful when you have statistical or aggregate operations to accomplish on a certain amount of data. In these and similar circumstances, there is just one value that you might want to return back to the caller. Because of its use cases, you normally use this method on more or less complex stored procedures rather than on single SQL statements.

ExecuteXmlReader builds up and returns an XmlReader object after a SELECT command that exploits XML features in SQL Server 2000 has been issued.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-20-2008, 09:28 PM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

ExecuteQuery from command Object

we can use the ExecuteNonQuery to perform catalog operations (Insert,Delete,Update)

ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.

executeQuery() is for command objects i.e., this method will send "select statement" to database and returns value given by the database.
Moreover the executeQuery() is not used in .net but it is used in JAVA.

executeNonQuery() uses the "Insert/Update/Delete/Stored subprogram" , this method will return number of records effected by the database table


Thanks
KiruthikaSambandam
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-30-2008, 02:08 AM
naina naina is offline
D-Web Trainee
 
Join Date: May 2008
Posts: 8
naina is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

executequery applies for commands and execute nonquery for insert,update and delete.
__________________
Web Templetes
Web site Design
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-21-2008, 11:16 PM
Miakoda Miakoda is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 49
Miakoda is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

ExecuteQuery from command Object

we can use the ExecuteNonQuery to perform catalog operations (Insert,Delete,Update)

ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-22-2008, 01:25 AM
stargold stargold is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 37
stargold is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

I agree with Kirubhananth!
__________________
QA Test Management
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-25-2008, 04:37 AM
symonds symonds is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 12
symonds is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language.

In many languages, such as DECLAN, C and similar environments like C++, a keyword is a reserved word which identifies a syntactic form. Words used in control flow constructs, such as if, then, and else are keywords. In these languages, keywords cannot also be used as the names of variables or functions.



Keyword Research Florida Health Insurance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 09-30-2008, 01:58 AM
Nicoleblack Nicoleblack is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 1
Nicoleblack is on a distinguished road
Default Re: difference between ExecuteQuery And Execute NonQuery

Hello,

ExecuteQuery from command Object

we can use the ExecuteNonQuery to perform catalog operations (Insert,Delete,Update)

ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.

Byee

Software development company
-----------------------------------------------------------------------------

Outsourcing software development
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/5454-difference-between-executequery-execute-nonquery.html
Posted By For Type Date
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 03-18-2008 10:10 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I execute a PHP script using command line? sundarraja PHP Programming 0 01-28-2008 02:53 AM
How can I execute an ASP page WITHOUT opening a window (hidden)? SaravananJ HTML, CSS and Javascript Coding Techniques 1 09-19-2007 12:11 AM
How to execute codes when TWO keys are pressed in Flash aramesh Flash Actionscript Programming 1 08-16-2007 02:44 AM
How to execute an external program from java? kingmaker Java Programming 1 07-30-2007 12:52 AM


All times are GMT -7. The time now is 11:35 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0