Jason Huang wrote:
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