EF6-ExecuteSqlCommandAsync-获取返回参数(声明标量变量错误) [英] EF6 - ExecuteSqlCommandAsync - Get return parameter (declare scalar variable error)

查看:441
本文介绍了EF6-ExecuteSqlCommandAsync-获取返回参数(声明标量变量错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

object[] parameters =
    {
        new SqlParameter("@val1", val1),
        new SqlParameter("@val2", val2),
        new SqlParameter
        {
            ParameterName = "@retVal",
            SqlDbType = SqlDbType.Int,
            Direction = ParameterDirection.ReturnValue,
            Value = -1
        }
    };

    await context.Database.ExecuteSqlCommandAsync("EXEC @retVal = Example_SP @val1, @val2", parameters);

我正在使用的SP很好,并且在SQL MS中返回了一个值.但是,当我使用EF执行它时,会告诉我必须声明标量变量@retVal".那不是我的SqlParameter做什么的吗?

The SP I'm using is fine and returns a value in SQL MS fine. But when I execute it using EF I am told I 'must declare the scalar variable @retVal'. Isn't that what my SqlParameter does??

正如某些人在其他地方建议的那样,我尝试从参数中删除"@"符号,但是据我所知,"@"符号是可选的,无论如何都没有区别.

I've tried removing the '@' sign form the parameters, as some have suggested elsewhere, but as I understand it the '@' sign is optional and makes no difference anyway.

如何使用ExecuteSqlCommandAsync从SP获取返回值而不会导致错误?

How do I get the return value from the SP without causing errors, using ExecuteSqlCommandAsync?

谢谢!

推荐答案

您需要使用ParameterDirection.Output而不是ParameterDirection.ReturnValue.

这篇关于EF6-ExecuteSqlCommandAsync-获取返回参数(声明标量变量错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆