在C#Dapper中从方法调用向SP发送DATETIME参数

时间:2016-10-07 11:23:18

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

我在SQL Server中有以下SP

CREATE PROCEDURE [dbo].[SPGetData] 

       @StartDate DATETIME,
       @EndDate DATETIME

AS
            BEGIN
                  SELECT *
                  FROM SampleTable
                  WHERE CAST(SampleTable.CreateDate AS DATE) BETWEEN  CAST(@StartDate AS DATE) AND CAST(@EndDate AS DATE)                   
            END
END
GO

当我从Management Studio测试此SP时,这工作正常,但我应该将StartDate和EndDate值传递如下

enter image description here

DECLARE @return_value int

EXEC    @return_value = [dbo].[SPGetStationData]
        @StartDate = N'2016/10/08',
        @EndDate = N'2016/10/09'

SELECT  'Return Value' = @return_value

GO

我想从后端c#程序发送参数,

如下创建的方法

    public IEnumerable<ModelClass> GetDetails(DateTime? startdate, DateTime? enddate)
    {
        using (SqlConnection cnn = this.OpenConnection())
        {

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@StartDate", startdate);
            parameters.Add("@EndDate", enddate);

            IList<ModelClass> SampleList = SqlMapper.Query<ModelClass>(cnn, "SPGetData", parameters, commandType: CommandType.StoredProcedure).ToList();

            cnn.Close();

            return SampleList.ToList();
        }
    }

但是当我调试这个时,我可以看到这个方法的值 采用以下格式

enter image description here

在存储过程中询问YYYY/MM/DD格式,但我在DD/MM/YYYY HH:MM:SS AM/PM格式

我应该怎么做才能接受SP格式

2 个答案:

答案 0 :(得分:0)

我的工作代码,

// Store procedure
CREATE PROCEDURE [dbo].[SPGetData] 
       @StartDate nvarchar(50) = null, -- change 
       @EndDate nvarchar(50) = null -- change 
AS
    BEGIN
       -- change  in where condition
       SELECT *
       FROM SampleTable
       WHERE 
        DATEPART(yyyy,cast(SampleTable.CreateDate as date)) BETWEEN DATEPART(yyyy,cast(@StartDate as date)) and DATEPART(yyyy,cast(@EndDate as date))
    END
END
GO

// Visual Studio Code for dapper
public IEnumerable<ModelClass> GetDetails(DateTime? startdate, DateTime? enddate)
    {

var _para = new DynamicParameters();
    _para.Add("@StartDate", startdate);
    _para.Add("@EndDate", enddate);
    var _list = _con.Query<ModelClass>("SPGetData", _para, commandType: CommandType.StoredProcedure); // _con is SqlConnection _con = new SqlConnection("your connection string")
            return _list;
    }

答案 1 :(得分:0)

以下代码适用于我。

BEGIN
   SELECT cat.id,cat.name, cate.name as parentname, cat.parent_id,
   cat.created_by, cat.created_date, cat.modify_by, cat.modify_date, 
   user.firstname, modifiedUser.firstname as modifiedUser
   FROM `category`as cat
   INNER JOIN `user` ON cat.created_by = user.id
   LEFT JOIN category as cate ON cate.id = cat.parent_id;
   JOIN `user`modifiedUser ON cate.modify_by = user.id
END

以下是程序:

void Main()
{
    DateTime? s=null;
    DateTime? e=DateTime.Today;
    Test(s,e);
}

void Test(DateTime? startDate, DateTime? endDate)
{
    var cs=@"data source=(local);database=test;integrated security=true";
    using (var con = new SqlConnection(cs))
    {
        con.Open();
        var t = con.Query<string>("TestIt", new { startDate, endDate},
                commandType: CommandType.StoredProcedure);

        $"List: {t.FirstOrDefault()}".Dump();
    }
}

请注意,与注释中的代码不同,我在传递参数时没有定义任何属性名称:

ALTER PROC TestIt
(
    @startDate datetime2,
    @endDate datetime2
)
AS
BEGIN
    SELECT 'Start: ' + CASE WHEN @startDate IS NULL THEN 'Null' ELSE CONVERT(VARCHAR(35), @startDate, 106) END +
            '\r\nEnd: ' + CASE WHEN @endDate IS NULL THEN 'Null' ELSE CONVERT(VARCHAR(35), @endDate, 106) END
END

由于过程参数名称与我传递给方法的变量相同。要使用与注释中相同的代码,可以将其修改为:

new { startDate, endDate}