数据库
首页 > 数据库> > c# – SqlDataReader和SQL Server 2016 FOR JSON将json拆分为2k字节的块

c# – SqlDataReader和SQL Server 2016 FOR JSON将json拆分为2k字节的块

作者:互联网

最近我玩了一些关于Azure SQL数据库的json auto功能的新功能.

当我使用此查询选择大量记录时:

Select
    Wiki.WikiId
    , Wiki.WikiText
    , Wiki.Title
    , Wiki.CreatedOn
    , Tags.TagId
    , Tags.TagText
    , Tags.CreatedOn
From
    Wiki
Left Join
    (WikiTag
Inner Join 
    Tag as Tags on WikiTag.TagId = Tags.TagId) on Wiki.WikiId = WikiTag.WikiId
For Json Auto

然后使用C#SqlDataReader进行选择:

var connectionString = ""; // connection string
var sql = "";  // query from above
var chunks = new List<string>();

using (var connection = new SqlConnection(connectionString)) 
using (var command = connection.CreateCommand()) {
    command.CommandText = sql;
    connection.Open();

    var reader = command.ExecuteReader();

    while (reader.Read()) {
            chunks.Add(reader.GetString(0)); // Reads in chunks of ~2K Bytes
    }
}

var json = string.Concat(chunks);

我收到了大量的数据.

为什么我们有此限制?为什么我们不把一切都放在一大块?

当我读取nvarchar(max)列时,我会将所有内容都放在一个块中.

谢谢你的解释

解决方法:

Format Query Results as JSON with FOR JSON开始:

Output of the FOR JSON clause

The result set contains a single column.

A small result set may contain a single row.

A large result set splits the long JSON string across multiple rows.
By default, SQL Server Management Studio (SSMS) concatenates the results into a single row when the output setting is Results to
Grid. The SSMS status bar displays the actual row count.

Other client applications may require code to recombine lengthy results into a single, valid JSON string by concatenating the
contents of multiple rows.
For an example of this code in a C#
application, see Use FOR JSON output in a C# client app.

我会说这完全是出于性能原因,类似于XML.更多SELECT FOR XML AUTO and return datatypesWhat does server side FOR XML return?

In SQL Server 2000 the server side XML publishing – FOR XML (see 07003) – was implemented in the layer of code between the query processor and the data transport layer. Without FOR XML a SELECT query is executed by the query processor and the resulting rowset is sent to the client side by the server side TDS code. When a SELECT statement contains FOR XML the query processor produces the result the same way as without FOR XML and then FOR XML code formats the rowset as XML. For maximum XML publishing performance FOR XML does steaming XML formatting of the resulting rowset and directly sends its output to the server side TDS code in small chunks without buffering whole XML in the server space. The chunk size is 2033 UCS-2 characters. Thus, XML larger than 2033 UCS-2 characters is sent to the client side in multiple rows each containing a chunk of the XML. SQL Server uses a predefined column name for this rowset with one column of type NTEXT – “XML_F52E2B61-18A1-11d1-B105-00805F49916B” – to indicate chunked XML rowset in UTF-16 encoding. This requires special handling of the XML chunk rowset by the APIs to expose it as a single XML instance on the client side. In ADO.Net, one needs to use ExecuteXmlReader, and in ADO/OLEDB one should use the ICommandStream interface.

标签:c,sql-server,tsql,azure-sql-database,sql-server-2016
来源: https://codeday.me/bug/20190701/1349322.html