如何将 DbContext.Database.SqlQuery(sql, params) 与存储过程一起使用? EF 代码优先 CTP5
我有一个具有三个参数的存储过程,我一直在尝试使用以下内容返回结果:
context.Database.SqlQuery<myEntityType>("mySpName", param1, param2, param3);
起初我尝试使用 SqlParameter
对象作为参数,但这不起作用并引发了SqlException
并显示以下消息:
过程或函数“mySpName”需要参数“@param1”,但未提供该参数。
所以我的问题是如何将此方法与需要参数的存储过程一起使用?
谢谢。
I have a stored procedure that has three parameters and I've been trying to use the following to return the results:
context.Database.SqlQuery<myEntityType>("mySpName", param1, param2, param3);
At first I tried using SqlParameter
objects as the params but this didn't work and threw a SqlException
with the following message:
Procedure or function 'mySpName' expects parameter '@param1', which was not supplied.
So my question is how you can use this method with a stored procedure that expects parameters?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您应该通过以下方式提供 SqlParameter 实例:
You should supply the SqlParameter instances in the following way:
另外,您可以使用“sql”参数作为格式说明符:
Also, you can use the "sql" parameter as a format specifier:
这个解决方案(仅)适用于 SQL Server 2005
你们是救星,但正如 @Dan Mork 所说,您需要将 EXEC 添加到其中。令我困惑的是:
定义(但不确定该位是必需的)。
:
This solution is (only) for SQL Server 2005
You guys are lifesavers, but as @Dan Mork said, you need to add EXEC to the mix. What was tripping me up was:
Definitions (not sure that bit is required though).
:
或
或
或
Or
Or
Or
大多数答案都很脆弱,因为它们依赖于 SP 参数的顺序。最好命名存储过程的参数并为其提供参数化值。
为了在调用 SP 时使用命名参数,而不用担心参数的顺序
将 SQL Server 命名参数与 ExecuteStoreQuery 和 ExecuteStoreCommand 结合使用
描述最佳方法。比 Dan Mork 的答案更好。
例如:
Most answers are brittle because they rely on the order of the SP's parameters. Better to name the Stored Proc's params and give parameterized values to those.
In order to use Named params when calling your SP, without worrying about the order of parameters
Using SQL Server named parameters with ExecuteStoreQuery and ExecuteStoreCommand
Describes the best approach. Better than Dan Mork's answer here.
E.g.:
或
或
或
or
or
or
我使用这种方法:
我喜欢它,因为我只需放入 Guid 和 Datetimes,SqlQuery 就会为我执行所有格式设置。
I use this method:
I like it because I just drop in Guids and Datetimes and SqlQuery performs all the formatting for me.
@Tom Halladay 的答案是正确的,提到您还应该检查 null 值并在 params 为 null 时发送 DbNullable ,因为您会得到类似
参数化查询 '...' 需要参数 '@parameterName' 的异常,没有提供。
这样的东西帮助了我
(该方法的功劳转到https://stackoverflow.com /users/284240/tim-schmelter)
然后像这样使用它:
或其他解决方案,更简单,但不通用是:
@Tom Halladay's answer is correct with the mention that you shopuld also check for null values and send DbNullable if params are null as you would get an exception like
The parameterized query '...' expects the parameter '@parameterName', which was not supplied.
Something like this helped me
(credit for the method goes to https://stackoverflow.com/users/284240/tim-schmelter)
Then use it like:
or another solution, more simple, but not generic would be:
我用 EF 6.x 做了这样的事情:
Don’t double up on sqlparameter 有些人对他们的变量这样做会被烧伤
I did mine with EF 6.x like this:
Don't double up on sqlparameter some people get burned doing this to their variable
当我调用一个带有两个输入参数并使用 SELECT 语句返回 3 个值的存储过程时,我遇到了相同的错误消息,并且我在 EF Code First Approach
UPDATE 中解决了如下问题:与 SQL SERVER 2005 一样,缺少 EXEC 关键字会产生问题。因此,为了允许它与所有 SQL SERVER 版本一起使用,我更新了我的答案并在下面的行中添加了 EXEC
I had the same error message when I was working with calling a stored procedure that takes two input parameters and returns 3 values using SELECT statement and I solved the issue like below in EF Code First Approach
UPDATE: It looks like with SQL SERVER 2005 missing EXEC keyword is creating problem. So to allow it to work with all SQL SERVER versions I updated my answer and added EXEC in below line