Varbinary (max) streaming data using Entity Framework 6

I have a SQL Server 2012 table containing file records. Each record has a column varbinary(max) BlobDatathat represents the data stored in the file. The data size can be more than 1 GB and cannot fit in RAM, so I don’t want it to be supported by byte. I would like to implement two streaming operations:

  • sequential reading of the string BlobDatain pieces to the buffer;
  • sequentially rewriting a string BlobDatain chunks using a buffer.

Using simple ADO.NET, an easy way to achieve this is to use SqlDataReaderand UPDATE .WRITE():

// 1. Sequentially read varbinary(max) into a buffer
using (SqlDataReader reader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
{   
    while (reader.Read())
    {
        byte[] buffer = new byte[size];

        while ((count = reader.GetBytes(0, offset, buffer, 0, buffer.Length)) > 0)
        {
            DoStuffWithBuffer(buffer);
            offset += count;
        }
    }
}

// 2. Sequentially overwrite varbinary(max) from a buffer - iterate:
UPDATE [File] SET [BlobData].WRITE(@buffer, @offset, @count)
WHERE [FileID]=@id

, , EF , ADO.NET. , , EF6 , EntityDataReader.GetBytes(), , MSDN, , , dataIndex, , , bufferIndex ".

  • , EntityDataReader.GetBytes() SqlDataReader.GetBytes() - ?

  • EF6 , UPDATE .WRITE(buffer, offset, count) varbinary(max)?

EF6, , EF6.

EF6, / EF6, ADO.NET, ? .NET 4.0.

+4
1

, - context.Database.ExecuteSqlCommand , ..

var id = 1234;
var blobStream = await someSource.ReadAsStreamAsync(); // obtain some stream
db.Database.ExecuteSqlCommand($"UPDATE MyTable set BlobColumn=@p0 
                                where Id=@p1", blobStream, id);
0

All Articles