jcscott

Read string stored in Blob field

Discussion created by jcscott on Sep 19, 2014

Hello,

 

I'm posting this here in case anyone else comes across this and needs a solution to reading string data stored in blobs.

 

First, i understand that this isn't the best method to be storing string/long text format, but it was best for my method and i suspect i'm not the only one who will want to do this!

 

In my case, i was required to store large text blocks into a geodatabase and wanted to keep everything contained into one simple row per-object.

I wasn't interested in either creating multiple rows to store a max of 255 chars or storing in another DB, so based on this i was able to store all my text in a Blob format.

 

I was a little stumped on how to read back this information in C# (python was a breeze), and there wasn't very much help i could find online for this.

So, with that in mind, the code below (with a little massaging) should be able to return a string store in a blob to a string type in C# if anyone is stuck.

 

 

private string getBlobText(IRow row, int blobrowint)
{
     //blobrowint is the integer location of the blobfield
     ESRI.ArcGIS.esriSystem.IMemoryBlobStream2 blobText = (ESRI.ArcGIS.esriSystem.IMemoryBlobStream2)row.get_Value(blobrowint);
     
     //creates a byte object equal to the size of the blob string
     int n = (int)blobText.Size;
     byte[] blobBytes = new Byte[n];

     //creates a variant of the blobtext as a object
     object blobObject = null;
     (commenttxt as ESRI.ArcGIS.esriSystem.IMemoryBlobStreamVariant).ExportToVariant(out blobObject);
     //converts the blobObject to a byte
     blobBytes = (byte[])blobObject;
     //translates the bytes to string
     string res = System.Text.Encoding.UTF8.GetString(blobBytes);
}

Outcomes