iT邦幫忙

0

WebApi 讀出 MSSQL 中日期的問題

資料表:
https://ithelp.ithome.com.tw/upload/images/20210610/201381620ncvtfslxb.jpg
https://ithelp.ithome.com.tw/upload/images/20210610/20138162r24djnnmgR.jpg
Web Api:

 public class OrderRequest
    {    .......
        public DateTime StarDate { get; set; }
        ........
    }
     public class PriceAfterChange
    {
        public DateTime Date { get; set; } //日期

        public int Price { get; set; } //價格
    }    
public IEnumerable<PriceAfterChange> Post([FromBody] OrderRequest request)
        {           
            //讀取 價格有變動的天數與日期
            string redchangeday = "Select [ChangeDate],[Price] from [RoomPriceAdjust] where [ChangeDate]>=@date";
              var a = this.Connection.Query<PriceAfterChange>(redchangeday, new { date = request.StarDate });
       
            return a;
        }

Call Api結果:
https://ithelp.ithome.com.tw/upload/images/20210610/20138162MFMPWBQ600.jpg

請問 為什麼讀出來的日期會是這樣子

謝謝各位

直接SQL查詢看看
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:18:21 檢舉
SQL查詢 日期都沒有問題
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
rogeryao
iT邦大師 1 級 ‧ 2021-06-10 11:18:19
最佳解答
"Select [ChangeDate] AS 'Date',[Price] from [RoomPriceAdjust] where [ChangeDate]>=@date"
看更多先前的回應...收起先前的回應...
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:31:32 檢舉

還是沒辦法

rogeryao iT邦大師 1 級 ‧ 2021-06-10 11:39:36 檢舉
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:41:33 檢舉

那個溢位剛剛有找到文章 後來發現是我傳值打錯而已 沒有溢位的問題了 謝謝

crag0911 iT邦新手 5 級 ‧ 2021-06-10 13:13:21 檢舉

-------------可以了----------------

 public class PriceAfterChange
    {
        public DateTime ChangeDate { get; set; } //日期 (原本是 Date 把它改成 ChangeDate)
        public int Price { get; set; } //價格
    }
   public IEnumerable<PriceAfterChange> Post([FromBody] OrderRequest request)
        {
            //讀取 價格有變動的天數與日期       
   
            string redchangeday = "Select [ChangeDate],[Price] from[RoomPriceAdjust] where [ChangeDate]>=@date";
            var a = this.Connection.Query<PriceAfterChange>(redchangeday,new { date=request.StartDate});     

            return a;
        }

有正確日期出現了

0
純真的人
iT邦大師 1 級 ‧ 2021-06-10 10:58:53

你SQL先轉型文字試試~

Select Convert(varchar,[ChangeDate],111) [ChangeDate],[Price] from [RoomPriceAdjust] where [ChangeDate]>=@date
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:04:26 檢舉

還是一樣

0
allenlwh
iT邦高手 1 級 ‧ 2021-06-10 11:19:31

抱歉 一時看錯,where條件應該沒問題,有問題的是輸出格式

看更多先前的回應...收起先前的回應...
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:31:51 檢舉

剛剛那個還是沒辦法

allenlwh iT邦高手 1 級 ‧ 2021-06-10 11:38:51 檢舉

只好一個一個來,先來試試直接取前三筆資料,不加 where條件

Select top 3 [ChangeDate],[Price] from [RoomPriceAdjust]
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:43:00 檢舉

Call Api後 回傳的值還是 "Date": "0001-01-01T00:00:00",

allenlwh iT邦高手 1 級 ‧ 2021-06-10 11:56:32 檢舉
 public class PriceAfterChange
    {
        public string Date { get; set; } //日期
        public int Price { get; set; } //價格
    }    
Select Top 3 Convert(varchar,[ChangeDate],111) as 'ChangeDate',[Price] from [RoomPriceAdjust]
crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:59:30 檢舉

PriceAfterChange裡面的Date型別改成 string 他回傳會變成Null

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-06-10 11:47:11

Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

雖然是 MySQL 的版本
不過試試也無妨

crag0911 iT邦新手 5 級 ‧ 2021-06-10 11:55:33 檢舉

還是不能

0
小魚
iT邦大師 1 級 ‧ 2021-06-10 18:27:46

你如果要這樣子的話,
要不要乾脆直接用EF,
用DataBase First的方式.

我要發表回答

立即登入回答