FOR JSON path returns less number of Rows on AZURE SQL

前端 未结 5 643
余生分开走
余生分开走 2020-12-11 19:57

I am using AZURE SQL (SQL Server 2016) and creating a query to give me output in JSON object. I am adding FOR JSON PATH at the end of query.

When I exe

5条回答
  •  臣服心动
    2020-12-11 20:31

    Separation of Concerns dictates returning a string and parsing the JSON separately. The below snippet can be used with no dependency on JSON.net which can be be used separately or a different Json Deserializer can be used (e.g. the one built into RestSharp) and does not require the SqlJSONReader class.

    try {
         using (var conn = new SqlConnection(connectionString))
         using (var cmd = new SqlCommand(sql, conn)) {
            await conn.OpenAsync();
            logger.LogInformation("SQL:" + sql);
            var rdr = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
            var result = "";
            var moreRows = rdr.HasRows;
            while (moreRows) {
                moreRows = await rdr.ReadAsync();
                if (moreRows) result += rdr.GetString(0);
            }
            return result;
         }
      }
      catch (Exception ex) {
         //logger.LogError($"Error accessing Db:{ex}");
         return null;
      }
    

提交回复
热议问题