【问题标题】:Stored procedure not making transaction存储过程不进行交易
【发布时间】:2016-07-07 15:37:57
【问题描述】:

我从我的上下文中调用了这个存储过程,但它不工作。然后我有其他正在运行的存储过程......它没有发送任何错误。

只是没有做出改变。

var id = facturavm.Id;

if (facturavm.EstadoAnterior == 0)
{
    var fecha = new DateTime(Convert.ToInt32(facturavm.Fecha[6].ToString() + facturavm.Fecha[7].ToString() + facturavm.Fecha[8].ToString() + facturavm.Fecha[9].ToString()), Convert.ToInt32(facturavm.Fecha[3].ToString() + facturavm.Fecha[4].ToString()), Convert.ToInt32(facturavm.Fecha[0].ToString() + facturavm.Fecha[1].ToString()));
    var x = DBManager.Context.ModificarFactura(facturavm.IdCliente, facturavm.IdProveedor, fecha, facturavm.MonedaDescripcion, facturavm.MonedaCambio, facturavm.Estado, facturavm.Total, id);
    DBManager.Context.SubmitChanges();
    DBManager.Context.ModificarFactura_Eliminar(id);
    DBManager.Context.SubmitChanges();
...

我的存储过程是:

(1) 那个不工作:

CREATE PROCEDURE [dbo].[ModificarFactura]
    @idCleinte int,
    @idProveedor int,
    @fecha date,
    @monedaDescripcion varchar(50),
    @monedaCambio float,
    @estado int,
    @total float,
    @id int
AS
    BEGIN TRY
    BEGIN TRAN 
        UPDATE Factura 
        SET IdCliente = @idCleinte, 
            IdProveedor = @idProveedor,
            Fecha = @fecha,
            MonedaDescripcion = @monedaDescripcion,
            MonedaCambio = @monedaCambio,
            Estado = @estado,
            Total = @total
        WHERE Id = @id

        COMMIT TRAN
    END TRY
    BEGIN CATCH
        ROLLBACK TRAN
    END CATCH
    RETURN 0

(2) 还有一个可行的:

CREATE PROCEDURE [dbo].[ModificarFactura_Eliminar]
    @idFactura int
AS
  BEGIN TRY
  BEGIN TRAN 
      DELETE FROM DetalleFactura 
      WHERE IdFactura = @idFactura;

      DELETE FROM ImpuestoProyectoFactura 
      WHERE IdFactura = @idFactura;

      DELETE FROM ProyectoFactura 
      WHERE IdFactura = @idFactura;

      DELETE FROM Impuesto 
      WHERE IdFactura = @idFactura;

      COMMIT TRAN
 END TRY
 BEGIN CATCH
      ROLLBACK TRAN
 END CATCH

 RETURN 0

谢谢大家!

【问题讨论】:

  • 在没有 try catch 的情况下运行它,你也许可以更正它
  • @ARUN 现在我得到了我的例外!谢谢
  • @ARUN 解决了​​!但它仍然没有进行 -ModificarFactura- 交易:(
  • @ARUN 已解决!谢谢!

标签: c# .net sql-server linq stored-procedures


【解决方案1】:

你不能任性地吞下所有错误并忽略它们。并且总是在 catch 块中回滚是无效的,没有先咨询XACT_STATE()Here 是一个正确的存储过程错误处理模式:

create procedure [usp_my_procedure_name]
as
begin
    set nocount on;
    declare @trancount int;
    set @trancount = @@trancount;
    begin try
        if @trancount = 0
            begin transaction
        else
            save transaction usp_my_procedure_name;

        -- Do the actual work here

lbexit:
        if @trancount = 0   
            commit;
    end try
    begin catch
        declare @error int, @message varchar(4000), @xstate int;
        select @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
        if @xstate = -1
            rollback;
        if @xstate = 1 and @trancount = 0
            rollback
        if @xstate = 1 and @trancount > 0
            rollback transaction usp_my_procedure_name;

        raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ;
    end catch   
end

【讨论】:

    【解决方案2】:

    试试这个

    CREATE PROCEDURE [dbo].[ModificarFactura]
        @idCleinte int,
        @idProveedor int,
        @fecha date,
        @monedaDescripcion varchar(50),
        @monedaCambio float,
        @estado int,
        @total float,
        @id int,
        @sts int output, --RETURN STATUS COMMIT OR NOT FROM SQL
        @error nvarchar(400) output --RETURN ERROR MESSAGE SQL
    AS
    BEGIN
        SET NOCOUNT ON;
        SET @sts = 1 ;
        SET @error = '';
        BEGIN TRY
            BEGIN TRANSACTION 
                UPDATE Factura 
                SET IdCliente = @idCleinte, 
                    IdProveedor = @idProveedor,
                    Fecha = @fecha,
                    MonedaDescripcion = @monedaDescripcion,
                    MonedaCambio = @monedaCambio,
                    Estado = @estado,
                    Total = @total
                WHERE Id = @id
            COMMIT TRANSACTION
        END TRY
        BEGIN CATCH
            IF @@ERROR > 0
                BEGIN
                    SET @sts = 0 ;
                    SET @error = ERROR_MESSAGE();
                    ROLLBACK TRANSACTION
                END
        END CATCH
    END
    GO
    

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2021-06-24
      相关资源
      最近更新 更多