How can I get the number of records affected by this statement:
select * from x_table where column1 = 5
I thought ExecuteNonQuery is what I need but it returned -1. I expected 2 because I have two records with column1 = 5 in my table. How do I get the correct count?
You’re calling
ExecuteNonQuery– but this is a query! No rows are affected by your statement because it’s just a query. You need to put the counting part into the query, like this:And then the simplest way of getting the result is to use
ExecuteScalar:You could just execute it as a reader and get the sole result, but
ExecuteScalaris simpler.