存储过程需要传递的参数

时间:2014-01-29 13:41:45

标签: sql sql-server sql-server-2008

从以下代码中我得到的错误是@StartDate未提供,但是逐步执行两个日期范围参数的值有效:

SqlParameter[] ps = new SqlParameter[]
{
    new SqlParameter("@StartDate", startDate),
    new SqlParameter("@EndDate", endDate)
};

List<AttendanceReportViewModel> res = db.Database.SqlQuery<AttendanceReportViewModel>(Local.queries["AttendanceReport"], ps).ToList();

return res;

存储过程:

ALTER PROCEDURE [dbo].[GetAttendanceReport]
    @StartDate datetime,
    @EndDate datetime
AS
BEGIN
    SET NOCOUNT ON;

    exec [REMOTE_SRV].LWD.dbo.ReportAttendance_sp @StartDate = @StartDate, @EndDate = @EndDate;
END

当我在SQL Server Management Studio中执行存储过程但未在应用程序中执行时,一切正常。

1 个答案:

答案 0 :(得分:1)

如果您的Local.queries["AttendanceReport"]看起来像这样:

Local.queries["AttendanceReport"] = "yourProc @StartDate, @EndDate"

然后试试这个:

List<AttendanceReportViewModel> res = db.Database.SqlQuery<AttendanceReportViewModel(
    Local.queries["AttendanceReport"], 
    new SqlParameter("StartDate", startDate),
    new SqlParameter("EndDate", endDate)
).ToList();