EntityFramework中的StoredProcedure日期时间错误

时间:2015-03-12 12:58:02

标签: c# sql datetime stored-procedures frameworks

在我的项目中,我使用了EntityFramework 5,我在上下文中使用了SP。喜欢这个

public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
{
    DataTable table = new DataTable();
    using (DbDataAdapter adapter = new SqlDataAdapter())
    {
        adapter.SelectCommand = _context.Database.Connection.CreateCommand();
        adapter.SelectCommand.Parameters.Clear();
        adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        adapter.SelectCommand.CommandText = "GetEvaluations";

        SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
        aid.Value = AgentId;
        SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date.ToString("dd.MM.yyyy"));
        SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date.ToString("MM.dd.yyyy"));
        SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
        fid.Value = FormId;

        adapter.SelectCommand.Parameters.Add(aid);
        adapter.SelectCommand.Parameters.Add(sd);
        adapter.SelectCommand.Parameters.Add(ed);
        adapter.SelectCommand.Parameters.Add(fid);

        adapter.Fill(table);
    }

    return table;
}

这里的DateTime格式不同,但只是这样工作。即使这两个参数也在查看表中的相同字段。

请查看日期时间格式(它们是不同的)我认为它们必须是相同的风格。

我的SP:

USE [EvaluationAssistt]
GO
/****** Object:  StoredProcedure [dbo].[GetEvaluations]    Script Date: 12.03.2015 11:25:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Hammit Yildirim
-- Create date: 11.03.2015
-- Description: Defined for Category Point Reports Performance
-- =============================================
ALTER PROCEDURE [dbo].[GetEvaluations]-- 258,'1.11.2014','11.24.2014',8
@AgentId int,
@StartDate Datetime,
@EndDate Datetime,
@FormId int
AS
BEGIN
    SET NOCOUNT ON;

    select fce.Id from FormsCallsEvaluated fce
        left join FormsCalls fc on fce.CallId = fc.CallId
        where fc.AgentId=@AgentId and fc.DateStarted > @StartDate and fc.DateStarted < @EndDate and fce.FormId=@FormId
END

我的问题是什么

我在下面尝试并将空表归还给我

 public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
    {
        DataTable table = new DataTable();
        using (DbDataAdapter adapter = new SqlDataAdapter())
        {
            adapter.SelectCommand = _context.Database.Connection.CreateCommand();
            adapter.SelectCommand.Parameters.Clear();
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.CommandText = "GetEvaluations";

            SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
            aid.SqlValue = AgentId;
            SqlParameter sd = new SqlParameter("@StartDate", SqlDbType.DateTime);
            sd.SqlValue = StartDate.Date;
            SqlParameter ed = new SqlParameter("@EndDate", SqlDbType.DateTime);
            ed.SqlValue = EndDate.Date;
            SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
            fid.SqlValue = FormId;

            adapter.SelectCommand.Parameters.Add(aid);
            adapter.SelectCommand.Parameters.Add(sd);
            adapter.SelectCommand.Parameters.Add(ed);
            adapter.SelectCommand.Parameters.Add(fid);

            adapter.Fill(table);
        }

        return table;
    }

就像这样工作SQL端

USE [EvaluationAssistt]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[GetEvaluations]
        @AgentId = 258,
        @StartDate = N'1.11.2014',
        @EndDate = N'11.24.2014',
        @FormId = 8

SELECT  'Return Value' = @return_value

GO

1 个答案:

答案 0 :(得分:3)

您已将@StartDate@EndDate列定义为DateTime,但您尝试将其传递给字符串值。

只需将正确的类型和值传递给您的参数,例如;

SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date);
SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date);