What I want to do:
Write a lot of small (average 500-1000 bytes) blob-like objects that I get from a queue to a SQL Server database, to a table that has a bigint
primary key, and the data as varbinary
column.
Currently I do it this way:
- get some object from a concurrent queue
- create a new memory stream, and a new binary writer
- serialize the object into the memory stream with the Write... command from the writer
- call
stream.ToArray()
and set the result as the parameter value of the SQL command - execute the command
This creates a lot of small byte arrays which will become garbage immediately after the write. I would like to avoid this, and only create one byte array, always write & read from that array.
But it seems like the SqlCommand
objects internally copy the buffer if you pass a byte array.
Also the problem of reusing the same byte array over and over is that I cant tell the SqlCommand
how many bytes it should write to the table (each object will have a slightly different length)
Any idea how to do this without writing my own SQL implementation ?