【问题标题】:Determine data size of row using SqlDataReader使用 SqlDataReader 确定行的数据大小
【发布时间】:2015-08-07 13:31:12
【问题描述】:

我正在尝试确定从 SQL Server 返回到 .NET 应用程序的任意 SQL 命令的数据量。列结构在编译时是未知的。我对数据本身不感兴趣,只对长度感兴趣。

long size = 0;
using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        //size += size of current reader row ?
    }
}

有什么办法吗?

【问题讨论】:

    标签: c# sql ado.net


    【解决方案1】:

    如果行的大小是指列的数量,则可以使用 reader.FieldCount 或 reader.VisibleFieldCount

    long size = 0;
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            size += reader.FieldCount;
        }
    }
    

    【讨论】:

    • 谢谢,但我是在收到的数据量之后。
    • 对不起,我想我不太了解你。也许您想通过在每一列上使用 reader.GetSqlBytes 来检索二进制数据以计算其长度(通过 reader.FieldCount 知道列的数量)...
    • 这基本上是我想要做的,但 GetSqlBytes() 似乎只有在 SQL 类型是一个 blob 类型的列开头时才有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多