获取 SQL table 的受影响行数

Get Count of Affected rows of SQL table

我想知道如何在使用 SqlDataAdapter class 时获取受影响的行数。这行 return 没有。受影响的行数?

adapter.UpdateCommand = command;

adapter.UpdateCommand 不执行查询,它只是设置用于更新的 SqlCommand,它没有 return 任何东西。

SqlCommand.ExecuteNonQuery returns 只是更新语句中受影响的行数:

int affectedRows = adapter.UpdateCommand.ExecuteNonQuery();

你也有相同的信息return编辑adapter.Update

int affectedRows = adapter.Update(dataSet);

SqlDataAdapter.UpdateCommand 的文档:

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

SqlCommand.ExecuteNonQuery

的文档

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

ExecuteNonQuery - returns 受影响的行数。

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