iT邦幫忙

0

Web Api 使用Dapper 的問題

  public class BaseResponse<T>
    {
        public bool status { get; set; }

        public string message { get; set; }

        public T result { get; set; }
    }
 public class RoomInfo
    {      
        public string ID { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string RoomImage { get; set; }
        public string Introduction { get; set; }
        public string Remarks { get; set; }       
        public int WeekdayPrice { get; set; }
        public int HolidayPrice { get; set; }
    }
 public BaseResponse<RoomInfo> GetAll()
        {
            BaseResponse<RoomInfo> G = new BaseResponse<RoomInfo>();
            
             var cu = Connection.Query<RoomInfo>("SELECT * FROM [dbo].[RoomInfo]");
            
            G.status = true;
            G.message = "成功";
            G.result = (RoomInfo)cu;
            return G;
        }

不好意思 請問我的Api 要回傳 BaseResponse<RoomInfo>
我該怎麼把Dapper查詢到的結果 放到RoomInfo裡

請問 那有辦法讓資料 是回傳這樣子的嗎?
{
"status": true,
"message": "查詢成功",
"result":
[
{
"ID": "....",
"Name":....,
"Type":....,
.....
},
{
"ID": "....",
"Name":....,
"Type":....,
.....
}
]
}

謝謝回答 我的問題解決了
這樣就能得到我想要的結果了

public BaseResponse<IEnumerable<RoomInfo>> Get()
        {
            string cmd = "SELECT * FROM [dbo].[RoomInfo]";

            BaseResponse<IEnumerable<RoomInfo>> R = new BaseResponse<IEnumerable<RoomInfo>>();
            R.status = true;
            R.message = "查詢成功";
            R.result = this.Connection.Query<RoomInfo>(cmd);          
            return R;
        }
skyksl066 iT邦新手 4 級 ‧ 2021-06-06 09:09:38 檢舉
Dapper 取回來的資料是list所以你要用list return 或是把想要的那筆取出來
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
PPTaiwan
iT邦好手 1 級 ‧ 2021-06-07 10:30:40
最佳解答

另一種方法可以參考一下

假設你是使用的 MSSQL 2008 以上版本 你可以直接在 SQL 裡面將資料直接導出 JSON

範例:

    DECLARE		
        @_OutBox_JSON					nvarchar(Max)	,
        @_OutBox_Data					nvarchar(Max)	


    SET	@_OutBox_JSON	='{
        "status": true,
        "message": "查詢成功",
        "result":=置換資料=}'

    SET	@_OutBox_Data=(
        SELECT	
            [Auot_SNO] AS	[SNO]			,
            [ServiceNAME] AS	[CNAME]			,
            [NowDateTime] AS	[Now_DateTime]	,
            [UPDDateTime] AS	[UPD_DateTime]			
        FROM [YOU TABLE]
        FOR JSON PATH, INCLUDE_NULL_VALUES
    )

    SET @_OutBox_JSON=REPLACE(@_OutBox_JSON,'=置換資料=',@_OutBox_Data)  ;

    SELECT @_OutBox_JSON

就可以從 SQL 內取得以下的 JSON

{
  "status":true,
  "message":"查詢成功",
  "result":[
    {
      "SNO":1,
      "CNAME":"A111",
      "Now_DateTime":"2020-11-09T04:24:45.563",
      "UPD_DateTime":"2021-06-07T03:18:23.260"
    },
    {
      "SNO":2,
      "CNAME":"A112",
      "Now_DateTime":"2020-11-09T04:26:43.100",
      "UPD_DateTime":"2021-06-07T03:18:21.840"
    },
    {
      "SNO":3,
      "CNAME":"A113",
      "Now_DateTime":"2020-11-18T13:15:40.327",
      "UPD_DateTime":"2021-06-07T03:18:22.743"
    }
  ]
}

以上方式,就不用宣告 public class RoomInfo ,但文件要寫 ...

從 SQL 導出/導入 JSON 我得到的好處

  1. c# 部份導出 JSON 不用再次宣告 Public class ,我用此方法很多的 TSQL 直接導出 JSON 給 Web , iOS , Android 所要的資料,我可以省下非常多在 C# 還要導入與導出 強型別 的結構宣告省下很多不需要的宣告模式與應用。

  2. TSQL 2008 以上的版本更可以支援讀取 JSON 的方法,只要導入的 JSON 是以「技術規格」來寫出來的話,那讀取上就有很多彈性的應用。

  3. 你的 TSQL 撰寫能力高等與熟悉,可以為每一個 TABLE 自動產生 強型別 結構宣告。當然,你的 Public class classname 的程式碼要寫到 TSQL 裡面,透過 TSQL 來組合一些程式碼就可以減輕很多應用。

  4. 你的 TSQL 也可以自行組合從其他 TSQL 產生的 JSON,例如其他的預存程式來組合整段你所希望的 JSON 結構。可以減少一些重覆要取得的 SELECT TABLE 的資料,但這方法你就要了解 SQL SERVER 預存程序如何寫。

  5. 如果還是要用到強型別,你可以將 JSON 丟到這個網站 https://app.quicktype.io/ 來幫你產生 Public class 例如以下
    quicktype.io

也可以減輕很多工作上的不斷的要宣告的強型別結構。不過,我透以 1~4 的方法第 5 點己經很少在用了,不過他很方便也可以多加參考。

我要發表回答

立即登入回答