繁体   English   中英

SQL Server回滚事务不起作用

[英]SQL Server Rollback transaction not working

我有以下存储过程:

CREATE PROCEDURE [dbo].[master_accounting_invoice_change]
(
    @accinvoiceuid uniqueidentifier,
    @invoicenumber nvarchar(50),
    @businessname nvarchar(150),
    @taxid nvarchar(20),
    @total money,
    @subtotal money,
    @taxamount money,
    @discountamount money,
    @invoicedate datetime,
    @createddate datetime,
    @newfolio int OUTPUT
)
AS
    IF NOT EXISTS (SELECT accinvoiceuid FROM dbo.accounting_invoice WHERE accinvoiceuid = @accinvoiceuid )
        BEGIN

            /* GET NEXT FOLIO FOR INVOICE */
            SELECT @newfolio = ISNULL(MAX(foliocurrent),0) + 1 
            FROM dbo.accounting_sender_folios
            WHERE accsenderuid = @accsenderuid
            AND isactive = 1;

            exec master_accounting_invoice_insert 
            @accinvoiceuid,
            @invoicenumber,
            @businessname,
            @taxid,
            @total,
            @subtotal,
            @taxamount,
            @discountamount,
            @comissionamount,
            @invoicedate,
            @createddate

            /* UPDATE NEXT FOLIO FOR INVOICE */
            UPDATE dbo.accounting_sender_folios
            SET foliocurrent = @newfolio
            WHERE accsenderuid = @accsenderuid
            AND isactive = 1;
        END
    ELSE
        BEGIN
            SET @newfolio = @folio;

            exec master_accounting_invoice_update 
            @accinvoiceuid,
            @invoicenumber,
            @businessname,
            @taxid,
            @total,
            @subtotal,
            @taxamount,
            @discountamount,
            @comissionamount,
            @invoicedate,
            @createddate
        END

现在,在我的C#应用​​程序中,为了保存更改,我调用了存储过程,但是问题是,发生异常时不会回退Foliocurrent ,然后更新并保存增量变量。

除了以下内容,所有内容都会回滚:

/* UPDATE NEXT FOLIO FOR INVOICE */
            UPDATE dbo.accounting_sender_folios
            SET foliocurrent = @newfolio
            WHERE accsenderuid = @accsenderuid
            AND isactive = 1;

这是C#应用程序中的代码。 回滚事务正在运行,但不会回滚增量作品集。

DbConnection conn = db.CreateConnection();
conn.Open();
DbTransaction trans = conn.BeginTransaction();

try{
  using (DbCommand cmd1 = db.GetStoredProcCommand("master_accounting_invoice_change"))
  {
    db.AddInParameter(cmd1, "accinvoiceuid", DbType.Guid, dr["accinvoiceuid"]);
    .....
    .....
    .....

    db.ExecuteNonQuery(cmd1);

    newFolio = Convert.ToInt32(db.GetParameterValue(cmd1, "newfolio"));
  }
}catch(Exception ex){
    // roll back transation
    trans.Rollback();
}

关于如何解决这个问题或为什么发生的任何线索?

提前感谢您的帮助。

亚历杭德罗

好吧,您可能在代码中的某处有它,但是您需要确保将命令与事务关联。 您还需要确保该命令与该事务关联到同一连接。 我不确定您的db.GetStoredProcCommand在做什么。

db.Connection = conn;
db.Transaction = trans;

暂无
暂无

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

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