views:

984

answers:

9

How can I go about storing a vb.net user defined object in a sql database. I am not trying to replicate the properties with columns. I mean something along the lines of converting or encoding my object to a byte array and then storing that in a field in the db. Like when you store an instance of an object in session, but I need the info to persist past the current session.

+2  A: 

You could use the BinaryFormatter class to serialize your object to a binary format, then save the resulting string in your database.

bdukes
+5  A: 

You can use serialization - it allows you to store your object at least in 3 forms: binary (suitable for BLOBs), XML (take advantage of MSSQL's XML data type) or just plain text (store in varchar or text column)

aku
+1  A: 

The XmlSerializer or the DataContractSerializer in .net 3.x will do the job for you.

lomaxx
+3  A: 

Before you head down this road towards your own eventual insanity, you should take a look at this (or one day repeat it):

http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx

Persisting objects in a database is not a good idea. It kills all the good things that a database is designed to do.

1800 INFORMATION
A: 

@aku, lomaxx and bdukes - your solutions are what I was looking for.

@1800 INFORMATION - while i appreciate your stance on the matter, this is a special case of data that I get from a webservice that gets refreshed only about once a month. I dont need the data persisted in db form because thats what the webservice is for. Below is the code I finally got to work.

Serialize

    #'res is my object to serialize
    Dim xml_serializer As System.Xml.Serialization.XmlSerializer
    Dim string_writer As New System.IO.StringWriter()
    xml_serializer = New System.Xml.Serialization.XmlSerializer(res.GetType)
    xml_serializer.Serialize(string_writer, res)

Deserialize

    #'string_writer and xml_serializer from above
    Dim serialization As String = string_writer.ToString
    Dim string_reader As System.IO.StringReader
    string_reader = New System.IO.StringReader(serialization)
    Dim res2 As testsedie.EligibilityResponse
    res2 = xml_serializer.Deserialize(string_reader)
Jas
A: 

What you want to do is called "Serializing" your object, and .Net has a few different ways to go about it. One is the XmlSerializer class in the System.Xml.Serialization namespace.

Another is in the System.Runtime.Serialization namespace. This has support for a SOAP formatter, a binary formatter, and a base class you can inherit from that all implement a common interface.

For what you are talking about, the BinaryFormatter suggested earlier will probably have the best performance.

Joel Coehoorn
A: 

I'm backing @1800 Information on this one.
Serializing objects for long-term storage is never a good idea

while i appreciate your stance on the matter, this is a special case of data that I get from a webservice that gets refreshed only about once a month.

It's not a matter of stances. It's because one day, you will change your code. Then you will try de-serialize the old object, and YOUR PROGRAM WILL CRASH.

Orion Edwards
A: 

@Orion Edwards

It's not a matter of stances. It's because one day, you will change your code. Then you will try de-serialize the old object, and YOUR PROGRAM WILL CRASH.

My Program will not "CRASH", it will throw an exception. Lucky for me .net has a whole set of classes dedicated for such an occasion. At which time I will refresh my stale data and put it back in the db. That is the point of this one field (or stance, as the case may be).

Jas
A: 

If it crashes (or throws an exception) all you are left with is a bunch of binary data to try and sift through to recreate your objects.

If you are only persisting binary why not just save straight to disk. You also might want to look at using something like xml as, as has been mentioned, if you alter your object definition you may not be able to unserialise it without some hard work.

abigblackman