为什么我的存储过程返回true,即使它引发错误?

时间:2017-03-06 06:28:04

标签: c# sql sql-server tsql ado.net

为什么这个SP还给我TRUE,即使我已经提到如果满足某些条件那么它应该引发错误但仍然返回true。我已经检查了条件和它的会面但仍然是真的,为什么?

SP:

ALTER PROC [dbo].[usp_UpdateWithdrawal]
(
    @NITNo int,
    @WorkNo int,
    @ContractorID int,
    @IsWithDrawn bit
)

AS
BEGIN
    Begin Try

     Begin Transaction

        Declare @ExpiryDate date
        Declare @ExpiryTime time(7)

        Set @ExpiryDate= (Select convert(varchar(11), ExpiryDate, 106) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)
        Set @ExpiryTime= (Select CAST(ExpiryTime as TIME(0)) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)

        IF(CONVERT(varchar(11), @ExpiryDate, 106)+' '+Convert(varchar(8),CAST(@ExpiryTime as TIME(0))) < CONVERT(varchar(11), Getdate(), 106)+' '+Convert(varchar(8),CAST(Getdate() as TIME(0))))       
        Begin
            RAISERROR('Sorry, Expiry Date and Time has passed', 16, 10);
            return;
        End

        Update AppliedWorks
        Set Withdrawn= @IsWithDrawn
        Where NIT_No= @NITNo and Work_No= @WorkNo and Contractor_ID= @ContractorID and 
        CONVERT(varchar(11), @ExpiryDate, 106)+' '+Convert(varchar(8),CAST(@ExpiryTime as TIME(0))) < CONVERT(varchar(11), Getdate(), 106)+' '+Convert(varchar(8),CAST(Getdate() as TIME(0)))

        --Update Hisotry

        Declare @AppliedWorkID int
        Set @AppliedWorkID= (Select AppliedWorkID from AppliedWorks 
                             Where NIT_No= @NITNo and Work_No= @WorkNo and Contractor_ID= @ContractorID)


                Insert into AppliedWorkHistory
                (
                    AppliedWork_ID,
                    FinancialMoney,
                    AboveBelow,
                    EntryDateTime,

                    TwoPercentAmount,
                    TwoPercentBankName,
                    TwoPercentDemandDaftNo,
                    TwoPercentDemandDaftValue,
                    TwoPercentRemarks,

                    EightPercentAmount,
                    EightPercentBankName,
                    EightPercentDemandDaftNo,
                    EightPercentDemandDaftValue,
                    EightPercentRemarks, 
                    IsFinalized,
                    Withdrawn
                )
                Select @AppliedWorkID, aw.FinancialMoney, aw.AboveBelow, Getdate(), aw.TwoPercentAmount, aw.TwoPercentBankName, aw.EightPercentDemandDaftNo,
                       aw.TwoPercentDemandDaftValue, aw.TwoPercentRemarks, aw.EightPercentAmount, aw.EightPercentBankName, 
                       aw.EightPercentDemandDaftNo, aw.EightPercentDemandDaftValue, aw.EightPercentRemarks, aw.IsFinalized, @IsWithDrawn  
                from AppliedWorks aw where aw.AppliedWorkID= @AppliedWorkID



     Commit Transaction

    End Try
    Begin Catch
                Rollback transaction
    End Catch 


END;

C#:

public static bool UpdateWithdrawal(int NitNo,int WorkNo,int ContractorID,bool IsWithDrawn) {     bool结果;

ClsDatabaseManager dbManager = ClsDatabaseManager.InitializeDbManager(Constants.Databases.TendersConnection);
try
{
    dbManager.CreateParameters(4);
    dbManager.AddParameters(0, "@NITNo", NitNo);
    dbManager.AddParameters(1, "@WorkNo", WorkNo);
    dbManager.AddParameters(2, "@ContractorID", ContractorID);
    dbManager.AddParameters(3, "@IsWithDrawn", IsWithDrawn);
    dbManager.Open();
    Result = dbManager.ExecuteNonQuery("usp_UpdateWithdrawal", CommandType.StoredProcedure).ToBool();
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    dbManager.Close();
    dbManager.Dispose();
}

return Result;

}

2 个答案:

答案 0 :(得分:2)

正如评论中所述。

您在TRY..CATCH内提出错误而未在CATCH处理错误,您也需要在CATCH部分提出错误。

答案 1 :(得分:1)

放入Raiserror语句后删除return语句 因为如果使用tryCatch块,Error会抛出阻止块 所以在raiseError之后删除商店过程中的return语句。