Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ExecuteNonQuery return 1 even if nothing was updated?

Tags:

c#

.net

mysql

I have a query like:

UPDATE messages SET Unread = 'N' WHERE id= '6'

To read affected rows, I use the value of ExecuteNonQuery(), but it always returns 1 even if nothing is changed. Saw the same problem here. Is this a bug or is this behavior normal?

like image 680
rtuner Avatar asked Dec 11 '22 13:12

rtuner


1 Answers

For ExecuteNonQuery to return 1 there must be a record WHERE id = '6'. Now, if you only want to update the row if the value is different then change the query:

UPDATE messages SET Unread = 'N' WHERE id = '6' AND Unread <> 'N'

If you were to run that query and the value of Unread was already 'N' then it would return 0 rows.

like image 118
Mike Perrenoud Avatar answered Mar 03 '23 04:03

Mike Perrenoud