Memory out exception when using sqldatareader for extracting bytes

Hello,
I have an varbinary column which stores bytes for an image.
The query is throwing an exception Out of memory .
I am using a Stored Procedure to get the query result.
This is the code:
  Public Sub Populate(ByVal Command As SqlCommand)
            'Accept command as string and fill the datatable with the result of sql query
            
            Dim dt = New DataTable
            Dim SQLdr As SqlDataReader
        Try

            SQLdr = Command.ExecuteReader(CommandBehavior.SequentialAccess)
            dt.BeginLoadData()
            dt.Load(SQLdr)
            dt.EndLoadData()

            Command.Connection.Close()
            Application.DoEvents()
            Command.Dispose()
            Data_Table = dt

        Catch MyException As SqlException
            MessageBox.Show("Stored procedure Error: MySQL code: " &
            MyException.Number & "  " &
            MyException.Message)
            End Try
        End Sub

Open in new window


Any suggestions are appreciated as I have been struggling with this for quite a while.

Thanks
RIASAsked:
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.

louisfrCommented:
Are you retrieving the whole table? How much data does the table contain?
0
Máté FarkasDatabase Developer and AdministratorCommented:
There 2 possible reasons:
1. Your table is too big (too many rows and/or too many columns)
2. Varbinary column is too big and DataTable cannot fetch all rows from the table.

My recommendations:
- Don't fetch all rows into a DataTable because it is very memory consuming operation
- Implement data processing in loop instead and fetch records of that table row by row (with DataReader)
1

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
RIASAuthor Commented:
Thanks.There is column of pictures saved as bytes , it has around 300 photos.
Thanks
0
Máté FarkasDatabase Developer and AdministratorCommented:
There is no good solution for that problem.
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
Visual Basic.NET

From novice to tech pro — start learning today.