MVC C#使用参数调用存储过程,第一个参数未被识别

时间:2012-04-16 22:51:16

标签: c# asp.net-mvc stored-procedures

有人可以请它向我解释一下吗?我有这个存储过程:

ALTER proc [dbo].[Invoice_GetHomePageInvoices] (
      @AreaIdList varchar(max)
,      @FinancialYearStartDate datetime = null
,      @FinancialYearEndDate datetime = null
) as

在代码中我试图这样称呼它:

var areasString = new SqlParameter("AreaIdList", "1,2");
var startDate = new SqlParameter("FinancialYearStartDate", financialYear.StartDate);
var endDate = new SqlParameter("FinancialYearEndDate", financialYear.EndDate);

return _db.Database.SqlQuery<HomePageInvoice>("Invoice_GetHomePageInvoices", areasString, startDate, endDate);

我收到了这个错误:

  

程序或功能'Invoice_GetHomePageInvoices'需要参数   '@AreaIdList',未提供。

说什么?我提供了那个参数。有人可以给我一些关于如何修复的见解吗?

2 个答案:

答案 0 :(得分:4)

您需要这样称呼它:

var areasString = new SqlParameter("AreaIdList", "1,2");             
var startDate = new SqlParameter("FinancialYearStartDate", financialYear.StartDate);             
var endDate = new SqlParameter("FinancialYearEndDate", financialYear.EndDate);              
return _db.Database.SqlQuery<HomePageInvoice>("EXEC Invoice_GetHomePageInvoices @AreaIdList, @FinancialYearStartDate, @FinancialYearEndDate", areasString, startDate, endDate);

我相信你也可以缩短它,比如:

return _db.Database.SqlQuery<HomePageInvoice>("EXEC Invoice_GetHomePageInvoices {0}, {1}, {2}", "1,2", financialYear.StartDate, financialYear.EndDate);

答案 1 :(得分:0)

可能需要参数值:

return _db.Database.SqlQuery<HomePageInvoice>("Invoice_GetHomePageInvoices", areasString.Value, startDate.Value, endDate.Value)