SQL将2个值返回给C#Web服务

时间:2016-06-13 08:26:51

标签: c# sql-server web-services

我有一个SQL存储过程,其工作原理如下:它将PaymentRecords表中的Confirmed列更新为1(已批准)或2(已拒绝),该表的默认值为0。为了确保并发控制,如果确认值已经为1或2,则会阻止更新。

当更新成功时,它返回一个字符串'SUCCESS',就好像它失败一样,它返回chequeNumber以显示一条消息,说明特定的检查已经更新。这两个都返回到C#Web服务。

SQL:

ALTER PROCEDURE [dbo].[UpdateUnverifiedChequeConfirmed] 
    -- Add the parameters for the stored procedure here
    @id uniqueidentifier,
    @confirmed int,
    @rejectReason nvarchar(MAX),
    @bankName nvarchar(MAX)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @existingConfirmed int;

    -- Insert statements for procedure here

    SELECT TOP 1 @existingConfirmed = [PaymentRecords].[confirmed] 
    FROM [dbo].[PaymentRecords] 
    INNER JOIN [dbo].[ChequePayments] 
    ON [ChequePayments].[paymentRecordID] = [PaymentRecords].[id]
    WHERE [ChequePayments].[id] = @id

    IF @existingConfirmed = '1' OR @existingConfirmed = '2'
        BEGIN
            PRINT 'Cheque already updated by another user, further changes not allowed.';

            --SELECT 0 AS FAIL
            SELECT [ChequePayments].[chequeNo] AS chequeNo
            FROM [ChequePayments]
            WHERE [ChequePayments].[id] = @id
        END
    ELSE
        BEGIN
            UPDATE [dbo].[PaymentRecords]
            SET confirmed = @confirmed, rejectReason = @rejectReason, [timestamp] = GETDATE()
            FROM [dbo].[PaymentRecords]
            INNER JOIN [dbo].[ChequePayments]
            ON [ChequePayments].[paymentRecordID] = [PaymentRecords].[id]
            WHERE [ChequePayments].[id] = @id

            UPDATE [dbo].[ChequePayments]
            SET bankName = @bankName, [timestamp] = GETDATE()
            WHERE [ChequePayments].[id] = @id

            --SELECT 1 AS SUCCESS
            SELECT 'SUCCESS' AS SUCCESS
        END
END

C#:

unverifiedChequeNumberResponse ICommon.updateUnverifiedCheque(unverifiedChequeRequest request, string id)
        {
            string result, rejectReason;

            Guid newID;

            if (request.bankName == "Select a Bank")
            {
                return new unverifiedChequeNumberResponse("Error: No bank selected.");
            }

            if (request.rejectReason == "")
            {
                rejectReason = null;
            }
            else
            {
                rejectReason = request.rejectReason;
            }

            if (Guid.TryParse(id, out newID))
            {
                try
                {
                    result = dbkl.UpdateUnverifiedChequeConfirmed(newID, request.confirmed, rejectReason, request.bankName).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    if (isDebug() == true)
                    {
                        return new unverifiedChequeNumberResponse(ex.Message);
                    }
                    else
                    {
                        return new unverifiedChequeNumberResponse("Error: Database inaccessible");
                    }
                }

                if (result == "SUCCESS")
                {
                    return new unverifiedChequeNumberResponse();
                }
                else
                {
                    unverifiedChequeNumberResponse response = new unverifiedChequeNumberResponse();
                    response.chequeNo = result;
                    return new unverifiedChequeNumberResponse("Error: Cheque " + response.chequeNo + " already updated by another user.");
                }
            }

            else
            {
                return new unverifiedChequeNumberResponse("Error: Invalid ID.");
            }
        }

虽然所有这些都正常工作,但我需要进行更改,以便SP在成功时返回1,在不成功时返回0。我面临的两个问题是:

  1. 如何使SP中的不成功更新路径返回2个值,即0和chequeNumber?如果我把它写成:

    SELECT 0 AS FAIL,[ChequePayments]。[chequeNo] AS chequeNo 来自[ChequePayments] WHERE [ChequePayments]。[id] = @id

  2. 它最终返回一个空值并抛出异常。

    1. 如果chequeNumber的值为1(实际上不太可能但仍值得覆盖)且更新被拒绝,则Web服务仍将返回成功消息。有没有办法改变它,以便服务可以区分1作为chequeNumber和1作为成功值?

1 个答案:

答案 0 :(得分:0)

您可以查看使用SQL Output parameters。例如;

ALTER PROCEDURE [dbo].[UpdateUnverifiedChequeConfirmed] 
    -- Add the parameters for the stored procedure here
    @id uniqueidentifier,
    @confirmed int,
    @rejectReason nvarchar(MAX),
    @bankName nvarchar(MAX),
    @result1 int output,
    @result2 varchar(50) output
AS
BEGIN

    -- do stuff...

    set @result1 = 123
    set @result2 = 'success'

END

您必须更新.Net代码以定义输出参数,然后在调用后读取它们的值。

您可能还想考虑使用事务进行并发控制 - 这就是它们的用途,它们将提供比您更强大的解决方案。