使用存储过程删除记录并获得返回结果到C#

时间:2016-09-15 09:41:40

标签: sql asp.net-mvc-4 stored-procedures

这里是我的C#代码

public DailyLossElements DeleteDailyLossElements(DailyLossElements _DOmodel)
    {
        decimal res = 0;
        try
        {
            var DOC = new SqlParameter("@DOC", _DOmodel.Daily_Opt_Code);
            var LossCategory = new SqlParameter("@LossCategory", _DOmodel.LossCategory);
            var Loss_Sub_Category = new SqlParameter("@Loss_Sub_Category", _DOmodel.Loss_Sub_Category);
            var Loss_Element = new SqlParameter("@Loss_Element", _DOmodel.Loss_Element);
            //var Result = new SqlParameter("@Result", _DOmodel.Loss_Element);

            OEEDBEntities oeedbEntities = new OEEDBEntities();
            res = oeedbEntities.Database.SqlQuery<decimal>("DeleteDailyLossElements @DOC,@LossCategory,@Loss_Sub_Category,@Loss_Element", DOC, LossCategory, Loss_Sub_Category, Loss_Element).FirstOrDefault();
            if (res != -999)
            {
                _DOmodel.IsActive = true;
                DeductFromDailyInput(_DOmodel.Daily_Opt_Code,res, _DOmodel.LossCategory, _DOmodel.Loss_Sub_Category);
            }

        }
        catch (Exception ex)
        {
            var excep = ex.Message;
        }
        return _DOmodel;

    }

这是我的存储过程

ALTER PROCEDURE [dbo].[DeleteDailyLossElements]
(@DOC int,@LossCategory nvarchar(50),@Loss_Sub_Category nvarchar(50),@Loss_Element nvarchar(50),@Result Decimal = -999 Output)

AS
Begin
DECLARE @LossElement_value as decimal 

if exists(SELECT LossElement_value from DailyLossElement where Daily_Opt_Code = @DOC AND LossCategory = @LossCategory AND Loss_Sub_Category = @Loss_Sub_Category AND Loss_Element = @Loss_Element )
Begin
SELECT @LossElement_value = LossElement_value from DailyLossElement where Daily_Opt_Code = @DOC AND LossCategory = @LossCategory AND Loss_Sub_Category = @Loss_Sub_Category AND Loss_Element = @Loss_Element 

Delete from DailyLossElement where Daily_Opt_Code = @DOC AND LossCategory = @LossCategory AND Loss_Sub_Category = @Loss_Sub_Category AND Loss_Element = @Loss_Element 

Set @Result= @LossElement_value;

End
else
Begin 
Set @Result=-999;
end
SELECT @Result
End

当我尝试从存储过程中检索返回结果并在c#中检查它没有工作。我还想返回值以发送另一个名为DeductFromDailyInput()的函数作为参数。

2 个答案:

答案 0 :(得分:1)

在您的过程中,您将变量@Result设置为输出参数。因此,在代码中,您必须具有sql参数@Result并用作ParameterDirection.Output

答案 1 :(得分:1)

将您的代码更改为以下内容:

public DailyLossElements DeleteDailyLossElements(DailyLossElements _DOmodel)
{
    decimal res = 0;
    try
    {
        var DOC = new SqlParameter("@DOC", _DOmodel.Daily_Opt_Code);
        var LossCategory = new SqlParameter("@LossCategory", _DOmodel.LossCategory);
        var Loss_Sub_Category = new SqlParameter("@Loss_Sub_Category", _DOmodel.Loss_Sub_Category);
        var Loss_Element = new SqlParameter("@Loss_Element", _DOmodel.Loss_Element);
        var Result = new SqlParameter("@Result", _DOmodel.Loss_Element) { Direction = ParameterDirection.Output };

        OEEDBEntities oeedbEntities = new OEEDBEntities();
        oeedbEntities.Database.SqlQuery<decimal>("DeleteDailyLossElements @DOC,@LossCategory,@Loss_Sub_Category,@Loss_Element", DOC, LossCategory, Loss_Sub_Category, Loss_Element, Result).FirstOrDefault();

        res = decimal.Parse(Result.Value.ToString());

        if (res != -999)
        {
            _DOmodel.IsActive = true;
            DeductFromDailyInput(_DOmodel.Daily_Opt_Code,res, _DOmodel.LossCategory, _DOmodel.Loss_Sub_Category);
        }

    }
    catch (Exception ex)
    {
        var excep = ex.Message;
    }
    return _DOmodel;

}

所以,我已经从头脑中更新了你的代码。可能我错过了一些东西,但它应该给你很好的想法从哪里开始。