Get Affected Rows on Executenonquery

Get affected rows on ExecuteNonQuery

ExecuteNonQuery - returns the number of rows affected.

SqlCommand comm;
// other codes
int numberOfRecords = comm.ExecuteNonQuery();

How do I check the number of affected rows on ExecuteNonQuery

Well, you can use a simple counter for that.

int counter = 0;
while(reader.Read())
{
...
...
if(GDR_EnumVoiePublique.ExecuteNonQuery() > 0)
{
counter++;
}
}

For INSERT statement, ExecuteNonQuery method returns the number of rows inserterd. That means; GDR_EnumVoiePublique.ExecuteNonQuery() > 0 lines execute your INSERT query and if it is successful, returns 1, if it is not, returns 0.

ExecuteNonQuery() Returns unexpected number of rows affected C#

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 being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

SQL Server 2008: Executenonquery not returning number of rows affected

I have solved it.

The culprit was the line I had put at the beginning of the stored procedure:

SET NOCOUNT ON;

By removing above line, now it is working perfectly, the number of rows affected is returned correctly to C#.

Get Count of Affected rows of SQL table

adapter.UpdateCommand doesn't execute the query, it just sets the SqlCommand for Updates and it doesn't return anything.

SqlCommand.ExecuteNonQuery returns just the number or affected rows in the update statement:

int affectedRows = adapter.UpdateCommand.ExecuteNonQuery();

Also you have the same information returned by adapter.Update

int affectedRows = adapter.Update(dataSet);

Docs for SqlDataAdapter.UpdateCommand:

Gets or sets a Transact-SQL statement or stored procedure used to
update records in the data source.

Docs for SqlCommand.ExecuteNonQuery

Executes a Transact-SQL statement against the connection and returns
the number of rows affected.



Related Topics



Leave a reply



Submit