Number of records affected by this expression

How can I get the number of records affected by this statement:

select * from x_table where column1 = 5

I thought that ExecuteNonQuerywas what I needed, but it returned -1. I was expecting 2 because I have two entries with column1 = 5in my table. How to get the right score?

+5
source share
5 answers

You call ExecuteNonQuery- but this is a request! Your line does not affect the lines because it is just a request. You need to put the counting part in the request, for example:

select count(*) from x_table where column1 = 5

And then the easiest way to get the result is to use ExecuteScalar:

int count = (int) command.ExecuteScalar();

, ExecuteScalar .

+8
select count(*) from x_table where column1=5

. , .

+7

ExecuteScalar ExecuteReader. ExecuteNonQuery , , , .

SELECT COUNT(1) FROM X_TABLE WHERE COLUMN1=5 , int rows = Convert.ToInt32(command.ExecuteScalar());

+4

SQL, ,

select count(*) from x_table where column1=5

, Linq - .

+1

, -1.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table inserted or updated, the return value includes the number of rows affected by the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of operators, the return value is -1 . If a rollback occurs, the return value is -1.

+1
source

All Articles