Persisting ESRI objects to the database
A little while ago, I was trying to persist some objects implementing the IPersistStream to the database. The database was MS SQL Server 2005 and the field type where the object was going to be stored was VARBINARY(MAX). The field was exposed in the DAL as an Byte array like it is supposed to be. So, now I had to write my objects implementing IPersistStream into an byte array. This took me a little while to figure out but turned to be simple enough. When, I had to do this before, I was writing to a table in a personal geodatabase. This was pretty straightforward since I was accessing and updating the storage table using ESRI’s ITable interfaces. In that case, I simply serialized my objects to a MemoryBlobStream and the wrote the MemoryBlobStream object into the table using the “Value” property on IRow. But this wouldn’t work for the current MS SQL server tables since I wasn’t accessing in through the ITable interface. So, I HAD to convert my MemoryBlobStream into an byte array. This is where the IMemoryBlobStreamVariant interface comes in handy. The following is how I was able to convert the MemoryBlobStream object to an byte array.
Dim s As MemoryBlobStream = New MemoryBlobStream()
Dim os As IObjectStream = New ObjectStream()
os.Stream = TryCast(s, ESRI.ArcGIS.esriSystem.IStream)
Dim content As Object = Nothing
Dim mvar As IMemoryBlobStreamVariant = TryCast(s, IMemoryBlobStreamVariant)
Dim bContent As Byte() = CType(content, Byte())