使用参数调用存储过程 [英] Calling stored procedure with parameters

查看:116
本文介绍了使用参数调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程返回一个值而不是一个数据集,我遇到问题可以使用EF4。

I have a stored procedure that returns a value, not a dataset, and I have problems getting it to work with EF4.

我已经看到: rel =nofollow noreferrer> http://dotnet.dzone.com/news/how-retrieve-stored-procedure

I've seen this: http://dotnet.dzone.com/news/how-retrieve-stored-procedure

这是我做了什么:我向模型添加了一个过程,并导入了一个函数。

Here is what I did: I added a procedure to the model, and imported a function.

我的第一个问题是我的过程中的参数被命名为 @_ Parameter_in 。这使EF将它们作为 p_Parameter_in 将它们作为第一个字符使用下划线。然后当我调用存储过程时,我可以在SQL Profiler调用中看到它查找 @p_Parameter_in ,当然有一个问题。

The first issue that I had was the fact that parameters in my procedure are named like @_Parameter_in. That made EF to bring them in as p_Parameter_in because it wouldn't work with an underscore as the first character. Then when I called the stored procedure, I can see in the SQL Profiler call that it looks for @p_Parameter_in, and of course there was an issue with that.

现在我重命名了这些参数,并研究了SQL Trace - 所有的外观和作品都非常棒。问题是我无法获得价值。这是我的代码如何:

Now I renamed the parameters and looked into SQL Trace - everything looks and works great. The problem is that I can't get the value out. Here is how my code looks:

System.Data.Objects.ObjectParameter newKey = new System.Data.Objects.ObjectParameter("NewKey_out", typeof(Int32));
newKey.Value = 0;
context.GetNextSurrogateKey_v2("tTest", newKey);

调用 newKey.Value 后,它总是0或我设置的任何值。它没有带回价值。我怀疑我的问题是如何导入功能。我使用Scalars和数据类型 Int32 。由于某种原因,我为禁用创建新的复杂类型任何人都有这个问题?

After calling newKey.Value, it's always 0 or whatever value I set it to. It doesn't bring the value back. I suspect my issue is with how I import the function. I use Scalars and data type Int32. "Create new Complex type" is disabled for me for some reason. Anybody had that issue?

推荐答案

我猜这里,因为我看不到你打电话的存储过程。我想你想要检索由存储过程返回的标量值,而不是存储过程中定义的输出参数的值。

I'm guessing here since I can't see the stored procedure that you're calling. I think you're wanting to retrieve a scalar value that has been returned by the stored procedure, rather than the value of an output parameter as defined in the stored procedure.

我相信你想在EDM中定义一个新的函数,并将其指向存储过程。一个快速的谷歌有这个潜在的解决方案: http:// www .devtoolshed.com / using-stored-procedures-entity-framework-scalar-return-values

I believe you want to define a new Function within the EDM and point this at the stored procedure. A quick google has this potential solution: http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values

HTH

这篇关于使用参数调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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