SQL Transaction Report / Status

I was looking for a way to get information about a completed SQL transaction. Since I use C # to connect to the database, I want to know how many records were updated, inserted, or deleted during a particular transaction. Is there any way to do this?

Thanks in advance

+4
source share
2 answers

ExecuteNonQuery () returns the number of rows affected.

    SqlCommand command = new SqlCommand(queryString, connection);
    command.Connection.Open();
    int rowsAffected = command.ExecuteNonQuery();

If you need multiple records, that is, the total number of records deleted, inserted, updated, etc. You will need to use the parameter OUTPUT.

        command.Parameters.Add("@DeletedRecords", SqlDbType.Int);
        command.Parameters["@DeletedRecords"].Direction = ParameterDirection.Output;

Then in a transactional stored procedure:

CREATE PROCEDURE [dbo].[TransactionReportStatus]
      @DeletedRecords INT OUTPUT
AS
BEGIN
     -- Your transaction with delete statements
     SET @DeletedRecords = @@ROWCOUNT         
END

@@ROWCOUNT SQL Server ExecuteNonQuery()

+1

, sql server, , , , , mysql, , . , . sql, , .

+1

All Articles