Streaming VARBINARY data from SQL Server in C#
See Download and Upload Images from SQL Server for an article covering the topic, including efficient streaming semantics. You must use a SqlDataReader
opened with CommandBehavior.SequentialAccess
:
SequentialAccess Provides a way for the DataReader to handle rows that contain columns with large binary values. Rather than loading the entire row, SequentialAccess enables the DataReader to load data as a stream. You can then use the GetBytes or GetChars method to specify a byte location to start the read operation, and a limited buffer size for the data being returned.
The linked article provides full code for creating a Stream backed by an SqlDataReader, you can simply Stream.CopyTo
(HttpResponse.OutputStream)
, or use a byte[] chunked copy if you don't have .Net 4.0 yet.
This follow up article explains how to use a FILESTREAM column for efficient streaming of large VARBINARY data in and out of the database.