What is the difference between excute query and excute nonquery.?

Showing Answers 1 - 6 of 6 Answers

nidhi

  • Sep 17th, 2005
 

Execute Query  is the  method which is used to  return the  result of the command like select Query. That is Update Query. The Execute Non Query

is used  to  return the Query os the statement  like the update Delete Insert which returns No data

  Was this answer useful?  Yes

Kalyan

  • Apr 16th, 2007
 

Dear nidhi,
There is no method named execute Query in Ado.net.There are four methods which can be applied on command objects, execureReader,ExecuteNonQuery,ExecuteScalar, and ExecuteXmlReader.I think the person who asked this question is unaware of this.

Thanks

  Was this answer useful?  Yes

Swathi

  • Jun 27th, 2007
 

Execute Query is mainly used for retrieving data i.e for the select statements and Execute non query is used for insert,update statements.

  Was this answer useful?  Yes

ravindra sah

  • Aug 2nd, 2007
 

Exec querey always shows data in grid but non query always shows information as messsage not in grid

  Was this answer useful?  Yes

BIZ

  • Aug 31st, 2007
 

ExecuteQuery will be used to select and Exectenonquery will be used for Insert/Update/Delete

  Was this answer useful?  Yes

swapnali mahajan

  • Aug 31st, 2007
 

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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions