【问题标题】:Execute stored proc without importing as a function执行存储过程而不作为函数导入
【发布时间】:2011-09-10 17:24:25
【问题描述】:

更新:

我想执行重新运行两个值的存储 proc 或 sql 命令。

我试图做一个简单的例子,使用ExecuteStoreCommand

首先我尝试发送纯 sql

var sqlReturn = repository.ExecuteStoreCommand("select count(*) as 'Count' FROM 
[mydb].[dbo].[mytable]"); //returns -1

但它总是返回-1

然后我创建了存储过程

CREATE PROCEDURE TestCount

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select count(*) as 'Count' FROM [mydb].[dbo].[mytable]
END
GO

但是当我打电话给我时,我得到了相同的-1 结果:

var spReturn = repository.ExecuteStoreCommand("exec dbo.TestCount"); //returns -1

那么有什么方法可以在实体框架中做到这一点,而无需将任何内容导入模型?

上面的代码只是一个例子,但是当我发现如何从 sql 中获取值时,查询会很复杂,我需要返回 count 和另一个计算值。

【问题讨论】:

  • 不需要引用计数字段吗?您的查询和 proc 正在返回结果集而不是返回值。
  • @Jeff O 问题已更新

标签: .net sql-server c#-4.0 entity-framework-4


【解决方案1】:

您需要使用ExecuteStoreQuery 方法来检索值。 ExecuteStoreCommand 将返回受影响的行数。如果您执行 Select 语句,则不会影响任何行。查看How to: Directly Execute Commands Against the Data Source 获取示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多