Vishful thinking…

Persisting ESRI objects to the database

Posted in .NET, ArcGIS, ESRI by viswaug on August 20, 2007

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)

        pitem.Save(os, 0)

 

        Dim content As Object = Nothing

        Dim mvar As IMemoryBlobStreamVariant = TryCast(s, IMemoryBlobStreamVariant)

        mvar.ExportToVariant(content)

        Dim bContent As Byte() = CType(content, Byte())

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: