none
getting Stream in and out of VarBinary(MAX) column? RRS feed

  • Question

  • I have some data that I am serializing.  I need to save and restore this from a VarBinary column using ADO.NET (also using Enterprise Library) under .Net 3.5 (not 4.0 yet).

    The only interface I seem to be able to find and get working is by using a byte[].  This requires allocating a large array to do the transfer.

    It seems impossible that ADO.NET doesn't support Streams for these columns.  Or am I overlooking something?

    Thanks

    Saturday, August 28, 2010 3:17 PM

Answers

  • Here is the example which you perhaps already found:

    http://msdn.microsoft.com/en-us/library/87z0hy49.aspx

    Like you said, it retrieves bytes into an array.  Although not a Stream, this is the building block you need to make a Stream.  It is possible to write a subclass of Stream that wraps the functionality.  Somebody must have done this already, though I haven't found an example yet.  It wouldn't be that hard...  Hard to believe it is not already in the framework...

    If your needs are simple you can use System.IO.MemoryStream as the go-between to convert between byte[] and Stream.  Unfortunately, that will buffer a complete copy of the data in memory, but that will be okay unless your blobs are very large.

     

    Saturday, August 28, 2010 3:53 PM

All replies

  • Here is the example which you perhaps already found:

    http://msdn.microsoft.com/en-us/library/87z0hy49.aspx

    Like you said, it retrieves bytes into an array.  Although not a Stream, this is the building block you need to make a Stream.  It is possible to write a subclass of Stream that wraps the functionality.  Somebody must have done this already, though I haven't found an example yet.  It wouldn't be that hard...  Hard to believe it is not already in the framework...

    If your needs are simple you can use System.IO.MemoryStream as the go-between to convert between byte[] and Stream.  Unfortunately, that will buffer a complete copy of the data in memory, but that will be okay unless your blobs are very large.

     

    Saturday, August 28, 2010 3:53 PM
  • Hi Chris, 

    I'm writing to follow up the post. Does the above BinaryCoder's suggestion work ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 2, 2010 9:54 AM
    Moderator