0

我在这个网站上找到了答案,但没有一个有效。我正在尝试从 ASP.NET MVC Web api 中的简单存储过程返回结果。

这有效:

 ctx.Database.SqlQuery <model>("StoredProc 1251");

但是,我一直认为像这样包含内联参数是不好的做法。

我没有尝试过其他任何工作 - 这里有一些例子:

//Assuming this variable:
int intValue;
intValue = 1251;

// None of these variations that I have tried work:
ctx.Database.SqlQuery<model>("StoredProc @intValue= {0}", intValue);
ctx.Database.SqlQuery<model>("StoredProc @IntValue", 1251);
ctx.Database.SqlQuery<model>("StoredProc @IntValue", new SqlParameter("@IntValue",1251);
 //And 6 or 7 others

都给我一个无用的 SQL Server 错误信息:

SP附近的语法不正确

什么是正确的语法?

还不能回答我自己的问题,但我找到了答案:

在 stackoverflow 的其他地方找到了正确的语法,或者至少一个正确的语法选项——这个网站真的很棒——我几乎总是在这里找到我的答案!

var myInt = new SqlParameter("@IntVal", SqlDbType.Int);
pMeme_ck.Value = 1251;
return ctx.Database.SqlQuery<ClaimSummary>("EXEC StoredProcedureName @IntVal=@IntVal", myInt);
4

1 回答 1

0

您正在对数据库执行原始 SQL 查询。您需要执行存储过程。

ctx.Database.SqlQuery<model>("EXEC StoredProc @IntValue", 1251);

更新:您可能还想尝试执行:

ctx.<EntityType>.SqlQuery("StoredProc @IntValue", 1251);

(替换为要返回的实体的类型名称。)

这应该返回由存储过程“StoredProc”返回的 EntityTypes。这个有限MSDN 文档。

于 2013-08-27T18:43:09.297 回答