获取SqlDataReader有效负载的大小

本文关键字:负载 SqlDataReader 有效 获取 | 更新日期: 2023-09-27 17:50:57

我试图找出从SQL Server返回的有效载荷的大小。我的代码是使用ADO。净:

using (var command = new SqlCommand("SELECT * FROM [dbo].[Products]", connection))
{
  SqlDataReader reader = command.ExecuteReader(); 
  // find # of bytes of payload returned

然而,我不确定如何获得从SQL服务器返回的大小。有办法用SqlDataReader来做吗?

获取SqlDataReader有效负载的大小

尝试下面的查询。将商店替换为表名,id替换为列名。

DECLARE @maxRowSize int
SET @maxRowSize =
(
SELECT SUM(DataSize) AS MaxRowSize
FROM
(
SELECT COLUMN_NAME, DATALENGTH(COLUMN_NAME) as DataSize
from information_schema.columns 
where table_name = 'shop' -- enter table name only, do NOT enter the schema
) AS SCHEM
)
SELECT COUNT(Id) as NumOfRows, --optional
@maxRowSize as MaxRowBytes, --optional
COUNT(id) * @maxRowSize as MaxQueryBytes
FROM Shop -- Enter schema name if needed