System.OutOfMemoryException when using SqlDataReader.GetBytes

I am losing my mind with this one. I am attempting to pull a HUGE file (41,656,832 bytes) from SQL Server and stream it to the client (browser) and keep running up against a mysterious "System.OutOfMemoryException" error.

CODE EXAMPLE:
 
Dim colIndex As Integer = 0                                         'objDR.GetOrdinal(m_sDocRepBinColumn)
Dim startIndex As Integer = 0, bufferSize As Integer = 4096         '1048576
Dim fileBuffer(bufferSize - 1) As Byte
Dim bytesInBuffer As Long

'Read into the buffer
'************* THIS VERY FIRST LINE CAUSES AN ERROR!!! ********************
bytesInBuffer = objDR.GetBytes(colIndex, startIndex, fileBuffer, 0, bufferSize)

'Write in the loop ONLY IF the bytes read exactly fill the buffer
Do While bytesInBuffer = bufferSize
    m_objHttpResponse.BinaryWrite(fileBuffer)
    m_objHttpResponse.Flush()

    startIndex += bytesInBuffer
    bytesInBuffer = objDR.GetBytes(colIndex, startIndex, fileBuffer, 0, bufferSize)
Loop

'Write out the remaining bytes, if any
If bytesInBuffer > 0 Then
    Dim lastFewBytes(bytesInBuffer - 1) As Byte
    Array.Copy(fileBuffer, 0, lastFewBytes, 0, bytesInBuffer)

    m_objHttpResponse.BinaryWrite(lastFewBytes)
    m_objHttpResponse.Flush()
End If

Open in new window


I am using the System.Data.SqlClient.SqlDataReader object and returning the BLOB (IMAGE column from SQL Server 2000) by itself (no other columns) and still this call fails with this error.

This same code seems to work a few times AFTER an IIS RESET, but eventually fails and continues to fail until another reset.

Smaller files (20,628,882 bytes) seem to work just fine with this same code.

Notice, also, that I started with a 1MB buffer (1024x1024) but reduced it to 4k (1024x4) just to get that first call to GetBytes() to succeed. (It's this first call that fails each time.)

I have searched EE & Google with NO LUCK.
PentegraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PentegraAuthor Commented:
Changing it to SequentialAccess seems to have done the trick for me.

*** CONSIDER THIS CLOSED! ***

Thanks!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PentegraAuthor Commented:
Calling the ExecuteReader method with the CommandBehavior.SequentialAccess argument did the trick for me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.