Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:59:02+00:00 2026-06-06T02:59:02+00:00

I have a SqlDataReader and need to read a varbinary(max) column from it using

  • 0

I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read.

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?

As far as I can see, the SqlDataReader doesn’t provide any methods to discover what length of data is available, therefore this method seems fairly awkward to me.

I’m tempted to just pass int.MaxValue here and forget about the issue but something about this doesn’t sit right with me.

I am aware that I can instead call

byte[] value = (byte[])dataReader["columnName"];

.. 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 SqlDataReader.GetXXXX() methods. So I am tied into using GetBytes and need to understand its proper usage.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:59:03+00:00Added an answer on June 6, 2026 at 2:59 am

    When dealing with varbinary(max), there are two scenarios:

    • the length of the data is moderate
    • the length of the data is big

    GetBytes() is intended for the second scenario, when you are using CommandBehaviour.SequentialAccess to ensure that you are streaming the data, not buffering it. In particular, in this usage you would usually be writing (for example) in a stream, in a loop. For example:

    // moderately sized buffer; 8040 is a SQL Server page, note
    byte[] buffer = new byte[8040]; 
    long offset = 0;
    int read;
    while((read = reader.GetBytes(col, offset, buffer, 0, buffer.Length)) > 0) {
        offset += read;
        destination.Write(buffer, 0, read); // push downstream
    }
    

    However! If you are using moderately sized data, then your original code:

    byte[] data = (byte[])reader[col];
    

    is fine!!. There is nothing wrong with this approach, and in fact the Get* API is broken in a few cases – GetChar() being a notable example (hint: it doesn’t work).

    It doesn’t matter that you have existing code that uses Get* – in this case, the cast approach is perfectly appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SqlDataReader to read data from SQL Server. However sometimes I want
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I need to iterate through a DataTable . I have a column there named
I have created a method that can be called using a stored procedure name
I have been playing with the demo code from this msdn article by Jeffrey
I need to read the values from a variable which is of type object
I need to call a wcf service method in my project, if I have
I have a method (GetDataReader, let's call it) that returns a SqlDataReader. It's inside
I have a basic/simple need to create a pipeline transfer process from one SQL
I'm with the follow question: I have a method that returns a SqlDataReader, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.