title: What 'length' parameter should I pass to SqlDataReader.GetBytes()
body: <p>I have a <code>SqlDataReader</code> and need to read a <code>varbinary(max)</code> column from it using the <code>SqlDataReader.GetBytes()</code> method. This method populates a byte array and therefore needs to know what length of data to read.</p>
  <p>This is where I get confused.. Clearly I want to read all the data that has been returned from the database in this row/column so what 'length' parameter should I pass?</p>
  <p>As far as I can see, the <code>SqlDataReader</code> doesn't provide any methods to discover what length of data is available, therefore this method seems fairly awkward to me.</p>
  <p>I'm tempted to just pass <code>int.MaxValue</code> here and forget about the issue but something about this doesn't sit right with me.</p>
  <p>I am aware that I can instead call</p>
  <pre><code>byte[] value = (byte[])dataReader[&quot;columnName&quot;];
  </code></pre>
  <p>.. and this seems to completely take care of the length issue internally.  However I am working with a set of complicated code generation templates that have been built around the <code>SqlDataReader.GetXXXX()</code> methods. So I am tied into using <code>GetBytes</code> and need to understand its proper usage.</p>