ThinkGeo.com    |     Blog    |     Wiki    |     Support

Storing object in MsSql2008FeatureLayer

Hi,

Building on my last post about the MsSql2008FeatureLayer, I’ve now been experimenting with saving entire objects to the database so that I can later deserialize them into memory and work with them and their associated features. The classes I am trying to save are abstract, so I can’t just create fields in the db for each value.

One of the limitations to this seems to be that you have to save strings to the MsSql2008FeatureLayer fields. As such, I found the following code snipits that will do the job of converting byte[] to string and vice versa.

In my test project, I have a test for this to ensure read after write is performing correctly when working with just the string values and not the database.

However, when running the same code to read from a record written on the database, the process fails. I can only assume it is something to do with the feature layer, the db field type, the encoding or some combination of the three.

I’ve attached my simple test project. You can create a db that replicates my test, add a feature to the db and then try to display it back. You can also verify my static read after write process. You just need to modify some of the conn properties for your default SQL db locations and instance.

Let me know if you can help on this.

DatabaseToMap.zip (56.4 KB)

Thanks,
Damian

Hi Damian,

Thanks for your test project.

The exception information is this:
Additional information: Binary stream ‘0’ does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.

I tried to implement ISerializable for your shot class, but still get this information, I think that maybe because some binary information is modified when saved to database.

I tried to trace this exception but it looks not easy to solve it. So I think we can find a workaround for it, we can implement another Serializer instead binary, for example XML or JSON.

For ThinkGeo object, you can directly use our GeoSerializer.

The sample code like this:

Serialize:

PointShape myPoint = new PointShape(10, 10);
MemoryStream myStream = new MemoryStream();

                GeoSerializer geoSerializer = new GeoSerializer();
                geoSerializer.Formatter.Encoding = Encoding.Unicode;
                geoSerializer.Serialize(myPoint, myStream);

                myStream.Position = 0;
                StreamReader reader = new StreamReader(myStream);
                string savedString = reader.ReadToEnd();

Deserialize:

GeoSerializer geoSerializer = new GeoSerializer();
geoSerializer.Formatter.Encoding = Encoding.Unicode;
PointShape point = geoSerializer.Deserialize(feature.ColumnValues[“MyObject”]) as PointShape;

And for the question in your code, we have to use string is because our columnValue for feature only support string now.

Wish that’s helpful.

Regards,

Don

Thanks Don.

That works.

Regards,
Damian

Hi Damian,

I am glad to hear that’s helpful.

Regards,

Don