Groups | Blog | Home
all groups > dotnet ado.net > august 2004 >

dotnet ado.net : SQLCommand.ExecuteNonQuery returns wrong number


Harry F. Harrison
8/31/2004 8:57:46 AM
I'm executing the .ExecuteNonQuery method of a SQLCommand object, with the
following SQL:

UPDATE BUDGET_ACCOUNT
SET Deleted = 1, DATE_DELETED = GetDate(), MODIFIED_BY_ID_FK = 1
WHERE ID = 951956

If I copy this SQL code to Query Analyzer, it returns 1 record affected.

However, if I run it in ADO.NET, it returns 2.

'ID' is the primary key, and is an auto numbered identity field...

I do not have a 'delete' trigger on the table.

Running VS 2003. SQL Server 2000 SP3. Win2K.

David Browne
8/31/2004 12:34:17 PM

[quoted text, click to view]

You could use this instead. Bind an output parameter to @ROWS.


UPDATE BUDGET_ACCOUNT
SET Deleted = 1, DATE_DELETED = GetDate(), MODIFIED_BY_ID_FK = 1
WHERE ID = 951956;
SET @ROWS = @@ROWCOUNT

David

Joyjit Mukherjee
9/1/2004 12:00:09 PM
Hi,

try with a SET NOCOUNT ON at the begining of the SP.

Regards
Joyjit

[quoted text, click to view]

Pablo Castro [MS]
9/1/2004 1:38:11 PM
Hi Harry,

It's the first time I hear about something like this. Is this something you
can reproduce in an stand-alone program? If so, I'd be glad to take a look
if you could post it to the newgroup or email me directly.

Thanks,

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


[quoted text, click to view]

Harry F. Harrison
9/2/2004 3:59:46 PM
I created a stand-alone sample, with code to create a new table, and was not
able to duplicate it. :)

After further investigation, I realized that I have an update trigger on the
table to update a field with the current date-time.

When I disabled the trigger, ExecuteNonQuery would return 1 - the same as
Query Analyzer.

So, I wonder why Query Analyzer is only returning 1,even when the trigger is
enabled.

Which result is 'correct'?


[quoted text, click to view]

Pablo Castro [MS]
9/7/2004 1:44:15 PM
hm, interesting. I don't know why the Query Analyzer folks decided to show
side-effects they way you mention below, but I'll try to find out.

SqlClient simply returns the sum of all the affected rows as reported by the
server.

As to which one is correct, well, I guess that's the kind of question you
can argue for both ways, depending on what are your particular needs or
scenario. In the next release (Whidbey), the connection has an event that
let's you get notified of each completed statements along with the number of
rows it affected.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


[quoted text, click to view]

AddThis Social Bookmark Button