簡體   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