如何从存储过程中获取值?

发布于 2024-12-28 03:26:52 字数 659 浏览 4 评论 0原文

ASP.NET MVC、VB.NET、SQL Server 2008 R2、Entity Framework v4

我创建了一个返回整数值的存储过程,

PROCEDURE [dbo].[spProfileCount] 
@STARTLETTER char(1) = 'A'
AS
BEGIN  
    SET NOCOUNT ON

    SELECT count(*) 
    FROM [Profils]
    WHERE LEFT(lastname,1) =  @STARTLETTER 

END

我在 EF 中导入了存储过程并创建了一个函数。在该函数中,我将返回集合定义为Int32类型的标量(这与我按[获取列信息]时看到的一致)代码> 按钮)。

那么这在控制器中是如何工作的呢?我尝试了很多事情但没有成功。

Dim n As Integer = 0 
n = context.spProfileCount("Q")

错误:

System.data.Objects.ObjectResults(整数?)' 类型的值不能 转换为“整数”。

ASP.NET MVC, VB.NET, SQL Server 2008 R2, Entity Framework v4

I created a stored procedure that returns an integer value

PROCEDURE [dbo].[spProfileCount] 
@STARTLETTER char(1) = 'A'
AS
BEGIN  
    SET NOCOUNT ON

    SELECT count(*) 
    FROM [Profils]
    WHERE LEFT(lastname,1) =  @STARTLETTER 

END

I imported the stored proc in EF and created a function. In that function I defined Returns a collection of as scalar of type Int32 (which is consistent with what I see when I press the [get column information] button).

So how does this work in the controller? I've tried many things without success.

I.e.

Dim n As Integer = 0 
n = context.spProfileCount("Q")

Error:

Value of type System.data.Objects.ObjectResults(of integer?)' can not
be converted to 'integer'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-04 03:26:52

您是否尝试过文档中显示的内容?

ObjectResult(Of T) 类

// 执行查询并获取ObjectResult。
对象结果<产品> queryResult = query.Execute(MergeOption.AppendOnly);

// 迭代 Product 项的集合。
foreach(queryResult 中的产品结果)
    Console.WriteLine("{0}", 结果.Name);

如果您确定只有一行,那么它将只是 queryResult.Single()

Have you tried what is shown in the documentation?

ObjectResult(Of T) Class.

// Execute the query and get the ObjectResult.
ObjectResult<Product> queryResult = query.Execute(MergeOption.AppendOnly);

// Iterate through the collection of Product items.
foreach (Product result in queryResult)
    Console.WriteLine("{0}", result.Name);

If you are sure you're going to have only one row, it's going to be just queryResult.Single()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文