473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ExecuteReader and ExecuteNonQuery

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
Dec 14 '06 #1
2 2578
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
Dec 14 '06 #2
"Jason Huang" <Ja************@hotmail.comschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP03.phx.gbl...
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
Dec 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hokes | last post by:
Hi All, We're using the 1.1. Framework against SQL Server 2000, and are having a strange issue where we don't get errors back from the stored procedure, i.e. the exception never gets thrown. ...
1
by: Mark | last post by:
It appears that you can only retrieve an Output parameter from a SQL Server stored procedure when using the ExecuteNonQuery of the SqlCommand class, and cannot use the ExecuteReader() method. In...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
6
by: BuddyWork | last post by:
Does anyone know if there are any articles explaining how I could debug into SqlCommand.ExecuteReader so I can see why I am getting a particular error. thanks
0
by: BuddyWork | last post by:
Hello, The problem is that when using SqlCommand.ExecuteReader and the SQL statement raises an error of severity of 16 then ExecuteReader throws an exception, when you use...
1
by: Francis | last post by:
Hello, My question is: is it better to use DataSets than the simple ExecuteNonQuery() methods. I thought the DataSets would be a better structured way of programming, but it seems to result in...
1
by: shahi | last post by:
Hi all, i have a sp that produce an error when execute by some special parameters. i write a program in vb.net that execute it, but i come accross a strange problem: when i execute the sp...
3
by: keithb | last post by:
What can I put in a stored procedure to control what gets returned by command.ExecuteNonQuery()? I already tried this: param = comm.CreateParameter(); param.ParameterName = "@Success"; ...
2
by: Tony Johansson | last post by:
Hello! If you do an update in the database you will use ExecuteNonQuery but the database update will work just as good if you instead use ExecuteReader or executeScalar. So my question is how...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.