Assuming that we have a stored procedure that does like something this:
BEGIN TRANSACTION
UPDATE sometable SET aField = 0 WHERE anotherField = 1;
UPDATE sometable SET aField = 1 WHERE anotherField = 2;
ROLLBACK TRANSACTION;
And from C# we have something like this:
using (var connection = new SqlConnection("connection string"))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "my_procedure";
var res = cmd.ExecuteNonQuery();
}
Why I'm not getting getting res == -1? I'm still getting the number of affected rows. When the documentation states "If a rollback occurs, the return value is also -1"
What I'm missing here?
It appears that the return value of
ExecuteNonQuery
is unaffected by a rollback even though the documentation clearly states that is does. Here are some possible workarounds.1) Use ExecuteScalar
SP:
C#
2) Use a return/output parameter
SP: DECLARE @RowCount INT DECLARE @Error INT
C#
3) Move the rollback/commit logic into the code
This would give you the ability to determine if a rollback occurred and output a value of -1 when necessary. The transaction statement would need to removed from the sproc.
SP:
C#:
As noted previously, the @@ROWCOUNT value and the ExecuteNonQuery result are both affected by triggers.