ExecuteReader and ExecuteNonQuery

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

I am not clear about the difference between the SqlCommand's ExecuteReader
and ExecuteNonQuery.
Would somebody show me an example to demonstrate the difference?
Thanks for help.


Jason
 
Jason said:
Hi,

I am not clear about the difference between the SqlCommand's
ExecuteReader and ExecuteNonQuery.
Would somebody show me an example to demonstrate the difference?
Thanks for help.

cmd.ExecuteNonQuery();

is exactly equivalent to

using (SqlDataReader rdr = cmd.ExecuteReader())
;

That is, any results returned by the command are simply ignored and the data
reader closed without even calling Read().

Generally, you'd use ExecuteNonQuery to execute any command that doesn't
return results, while you'd use ExecuteReader to execute a command which
returns results that you want to examine one at a time. If you simply need
all of the results in a DataSet, use a SqlDataAdapter instead of calling
ExecuteDataReader directly.

-cd
 
Jason Huang said:
Hi,

I am not clear about the difference between the SqlCommand's ExecuteReader
and ExecuteNonQuery.
Would somebody show me an example to demonstrate the difference?
Thanks for help.


Jason
Hi Jason,

ExecuteNonQuery is for commands that don't return any result like inserts or
updates
 
Back
Top