简体   繁体   中英

Wrong date when using SQL Server stored procedure with Dapper

I am using a stored procedure with Dapper to retrieve data from a table. The stored procedure works fine when executed in SQL Server and returns the required information.

But when I use Dapper to run the stored procedure and retrieve a Date , the Date is returned as 01/01/0001 .

Here is my stored procedure which works perfectly in SQL Server:

ALTER PROCEDURE [dbo].[spRankings_GetByEventAndGender]
   @Event varchar(50),
   @Gender varchar(10)
AS
BEGIN
    DECLARE @event_factor INT = CASE
                                   WHEN @Event IN ('Javelin', 'Discus', 'Shot Put', 'Hammer', 'Long Jump', 'High Jump', 'Triple Jump', 'Pole Vault')
                                      THEN -1 /* reverse ranking = highest value is best */
                                      ELSE 1  /* normal ranking = lowest value is best */
                                END;

    SELECT 
        CASE
           WHEN a.mark = ABS(b.mark)
              THEN CAST(b.rank AS VARCHAR)
              ELSE ''
        END AS [Rank],
        /*
         ,a.athleteid
         ,a.teamid
         ,a.eventid
        */
        CASE
           WHEN @event_factor = -1
              THEN LTRIM(STR(a.mark, 10, 2))
              ELSE FORMAT(DATEADD(SECOND, FLOOR(a.mark), 0),case when a.mark<60 then '' else 'm:' end+'ss')
                 +substring(ltrim((str(cast(a.mark as decimal(12,5))%1,10,2))),2,10)
          end as Mark
         ,a.wind as Wind
         ,d.eventname as [Event]
         ,c.firstname+' '+c.lastname as Athlete
         --,Convert(varchar(10),c.BirthDate,103) as [Birth Date]
         ,c.BirthDate as [BirthDate]

         ,e.teamname as [Team]
         ,a.venue as Venue
         --, Convert(varchar(10),a.PerformanceDate,103) as [Performance Date]
         ,a.PerformanceDate as [Performance Date]
     from dbo.Performances as a
          inner join (select a.PersonId
                            ,a.eventid
                            ,min(a.mark*@event_factor) as mark
                            ,rank() over(partition by a.eventid order by min(a.mark*@event_factor)) as [rank]
                            ,avg(a.mark) as avg_mark
                        from dbo.Performances as a
                             inner join dbo.Persons as b
                                     on b.PersonId=a.PersonId
                             inner join dbo.[Events] as c
                                     on c.eventid=a.eventid
                             inner join dbo.Meets as d
                                    on d.MeetId = a.MeetId
                       where b.gender=@Gender
                         and c.eventname=@Event
                       group by a.PersonId
                               ,a.eventid
                     ) as b
                  on b.eventid=a.eventid
                 and b.PersonId=a.PersonId
          inner join dbo.Persons as c
                  on c.PersonId=a.PersonId
          inner join dbo.events as d
                  on d.eventid=a.eventid
          inner join dbo.teams as e
                  on e.teamid=a.teamid
            inner join dbo.Meets as m
                on m.MeetId = a.MeetId

    order by a.eventid
            ,a.mark*@event_factor
            ,b.[rank]
/*
            ,b.avg_mark
            ,a.athleteid
*/
end

The results in SQL Server:

我的存储过程结果在SQL Server中

The method that uses Dapper to get the results:

public List<RankingsModel> GetRankingsByEventAndGender(string eventName, string gender) {
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.GetConString("GARanks"))) {
        var output = connection.Query<RankingsModel>($"dbo.spRankings_GetByEventAndGender @Event, @Gender", new { Event=eventName, Gender=gender}).ToList();
        return output;
    }
}

The results in my application:

我申请中的结果

You are getting the default value in the application. Please find below links which can help:

Date is 01/01/0001 and not the date that is in the database?

DateTime shows the date as 01/01/0001

HTH

Thanks.

You can probably break the problem down into pieces. First make sure that Dapper is returning the right result set. You could

connection.Query<Object>

or

connection.Query<dynamic>

and inspect the result to see if it has the right DateTime value in it, in debug mode. If it does, then the problem isn't dapper retrieving the results from the database, it's that it can't map it to your POCO/class.

If the result dataset doesn't have the right value of Performance Date , then the problem is with the sql generation that Dapper is doing, and you could look into renaming the column to be without spaces, or pursue that avenue. Good luck!

You probably have a property named 'PerformanceDate' in RankingsModel class which does not get mapped with 'Performance Date' returned from DB call.

Kindly update the SQL to return 'PerformanceDate' or make same name for both.

The other option is using Column Mapping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM