为什么这个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;
}
答案 0 :(得分:2)
正如评论中所述。
您在TRY..CATCH
内提出错误而未在CATCH
处理错误,您也需要在CATCH
部分提出错误。
答案 1 :(得分:1)
放入Raiserror语句后删除return
语句
因为如果使用tryCatch
块,Error
会抛出阻止块
所以在raiseError
之后删除商店过程中的return语句。