4

I am trying to determine the amount of data returned to a .NET application from SQL Server for an arbitrary SQL command. The column structure is not known at compile-time. I'm not interested in the data itself, just the length.

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

Is there any way to do this?

geofftnz
  • 9,954
  • 2
  • 42
  • 50

1 Answers1

0

If with size of the row you mean the amount of columns, you could use reader.FieldCount or reader.VisibleFieldCount

long size = 0;
using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        size += reader.FieldCount;
    }
}
Blas Soriano
  • 566
  • 3
  • 16
  • Thanks, but I was after the quantity of data received. – geofftnz May 26 '15 at 04:14
  • Sorry, I think I don't understand you well. Maybe you want to retrieve binary data to calculate its length by using reader.[GetSqlBytes](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getsqlbytes(v=vs.110).aspx) on each column (known the amount of columns with reader.FieldCount)... – Blas Soriano May 26 '15 at 04:24
  • That's basically what I'm trying to do, but GetSqlBytes() only appears to work if the SQL type is a blob-type column to begin with. – geofftnz May 26 '15 at 04:33