2

I would like to know how can I get the count of affected rows when using the SqlDataAdapter class. Does this line return the no. of affected rows?

adapter.UpdateCommand = command;
Shiza Khan
  • 51
  • 5

2 Answers2

0

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.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

ExecuteNonQuery - returns the number of rows affected.

SqlCommand comm;
// other codes
int numberOfRecords = comm.ExecuteNonQuery();
Slan
  • 545
  • 2
  • 6
  • 18