Search code examples
c#entity-frameworkvarbinary

Entity Framework and appending VARBINARY field


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


Solution

  • 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