C# SQL Server回滚事务不工作
我有以下存储过程:C# SQL Server回滚事务不工作,c#,sql,sql-server,sql-server-2008,transactions,C#,Sql,Sql Server,Sql Server 2008,Transactions,我有以下存储过程: CREATE PROCEDURE [dbo].[master_accounting_invoice_change] ( @accinvoiceuid uniqueidentifier, @invoicenumber nvarchar(50), @businessname nvarchar(150), @taxid nvarchar(20), @total money, @subtotal money, @taxamoun
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();
}
关于如何解决这个问题或者为什么会发生这种情况,有什么线索吗
提前感谢您的帮助
Alejandro嗯,您可能在代码中的某个地方有它,但是您需要确保将该命令与事务关联起来。您还需要确保该命令与事务所在的同一连接相关联。我不确定db.getstoredproc命令在做什么
db.Connection = conn;
db.Transaction = trans;
嗯,您可能在代码中的某个地方有它,但是您需要确保将该命令与事务关联起来。您还需要确保该命令与事务所在的同一连接相关联。我不确定db.getstoredproc命令在做什么
db.Connection = conn;
db.Transaction = trans;
我忘记将事务添加到db.ExecuteNonQuery。感谢rememberI忘记将事务添加到db.ExecuteNonQuery。谢谢你的记忆