简体   繁体   English

SQL事务报告/状态

[英]SQL Transaction Report/Status

I've been searching for a way to get information about a completed SQL transaction. 我一直在寻找一种方法来获取有关已完成的SQL事务的信息。 Since I'm using C# to connect to a DB I want to know how many records were updated, inserted or deleted during a specific transaction. 由于我使用C#连接到数据库,因此我想知道在特定事务期间有多少记录被更新,插入或删除。 Is there any way to do this? 有没有办法做到这一点?

Thanks in advance 提前致谢

ExecuteNonQuery() returns the number of rows affected. ExecuteNonQuery()返回受影响的行数。

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

If you want multiple records, ie the total number of records deleted, inserted, updated etc. You would have to use an OUTPUT parameter. 如果您想要多条记录,即删除,插入,更新等记录的总数。您必须使用OUTPUT参数。

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

Then in your transactional stored procedure: 然后在您的事务存储过程中:

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

@@ROWCOUNT is SQL Server's equivalent of ExecuteNonQuery() @@ ROWCOUNT是SQL Server相当于ExecuteNonQuery()

Note that in sql server rows affected for an update statement tells you how many rows meet the selection criteria, not the count of rows that were actually changed in contrast to mysql which returns the number of rows that were actually changed. 请注意,在sql server中,对update语句有影响的行会告诉您有多少行符合选择条件,而不是与mysql相比实际更改的行数,而mysql返回实际更改的行数。 I prefer to know the number of rows actually changed. 我更喜欢知道实际更改的行数。 If there is a way to do that in sql server, I would like to know how. 如果在sql server中有办法做到这一点,我想知道如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM