1
votes

i am trying to add bytes of a file to a field in the database which is of type VARBINARY bu this needs to be appended due to file size constraits

Is there any example code/website of how to do this? Or is it even possible to append the bytes to this field using Entity Framework?

I need to append the data as getting a byte array of 1GB + is going to cause memory exceptions so I think this is the only way..

Some code I have done

using (var stream = File.OpenRead(fn))
{
   long bytesToRead = 1;
   byte[] buffer = new byte[bytesToRead];

    while (stream.Read(buffer, 0, buffer.Length) > 0)
    {
        Item = buffer;
    }
 }

Thanks for any help

2

2 Answers

1
votes

The basic idea is making an stored procedure that implements an update like this:

UPDATE MyTable SET Col = Col + @newdata WHERE Id = @Id

and invoking it using ExecuteSqlCommand (see MSDN docs here).

But in this case you're only transfering the problem to the SQL Server side The column must be retrieved, modified, and written back).

To really get rid of the memory problem, implement your stored procedure using UPDATETEXT, which is much more efficient for your requirements:

Updates an existing text, ntext, or image field. Use UPDATETEXT to change only a part of a text, ntext, or image column in place. Use WRITETEXT to update and replace a whole text, ntext, or image field

0
votes

When storing large files in a database, it is usual to store the file on disc on the Web Server rather than in the database. In the database you store the path to the file, thus your code can get to it's contents without having to store gigs of data in the database.

However, if you are attempting to manipulate the contents of a 1GB+ file in memory, this is going to be interesting however you do it...