In my experience, System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
will return the sum of the rows affected by the batch as long as the session setting is SET NOCOUNT OFF
(the default). A negative value, -1, will be returned only when SET NOCOUNT ON
is specified. The row count value returned should be the same whether the SqlCommand.ExecuteNonQuery()
is invoked directly or via the base type System.Data.Common.DbCommand.ExecuteNonQuery()
base type (as in your case).
Are you reusing the same connection for different queries? If so, perhaps a previous command executed SET NOCOUNT ON;
and the setting was still effective when the MERGE
statement was executed on the same connection. Check for SET NOCOUNT ON;
in other queries that might use the same connection.