c# - Entity Framework 日期时间错误中的存储过程

标签 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 格式不同,但就是这样工作的。即使这 2 个参数也在查找表中的相同字段。

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

我的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

最佳答案

您将 @StartDate@EndDate 列定义为 DateTime 但您尝试向它们传递字符串值。

只需将正确的类型和值传递给您的参数即可;

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

关于c# - Entity Framework 日期时间错误中的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29010453/

相关文章:

mysql - 将字符串 "September 23"转换为 DATETIME

c# - 在 finally block 中访问函数返回的值

C#/.NET - 使用异步/等待函数调用时 CPU 使用率非常高

c# - 为什么 MethodImplOptions.Synchronized 在不推荐的情况下导致锁定?

c# - 如何列出 MVC 用户及其角色?

php - MySQL 查询适用于服务器但不适用于 PHP

mysql - cakephp mysql,自动增量 id 或 varchar 作为主键

php - 请调试 MySQL 语法错误

java - 日期时间转换为日期时显示不同的日期

python - 从列表中获取最接近的日期时间