<p>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. </p>

<hr>

<p>@Orion Edwards</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>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).</p>

[{'answer_id': 40888, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 2, 'selected': False, 'text': '<p>You could use the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx" rel="nofollow noreferrer">BinaryFormatter</a> class to serialize your object to a binary format, then save the resulting string in your database.</p>\n'}, {'answer_id': 40893, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 4, 'selected': True, 'text': '<p>You can use <a href="http://msdn.microsoft.com/en-us/library/ms973893.aspx" rel="nofollow noreferrer">serialization</a> - 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) </p>\n'}, {'answer_id': 40909, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': '<p>The XmlSerializer or the DataContractSerializer in .net 3.x will do the job for you.</p>\n'}, {'answer_id': 40917, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 2, 'selected': False, 'text': '<p>Before you head down this road towards your own eventual insanity, you should take a look at this (or one day repeat it):</p>\n\n<p><a href="http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx" rel="nofollow noreferrer">http://thedailywtf.com/Articles/The-Mythical-Business-Layer.aspx</a></p>\n\n<p>Persisting objects in a database is not a good idea. It kills all the good things that a database is designed to do.</p>\n'}, {'answer_id': 41126, 'author': 'Jas', 'author_id': 777, 'author_profile': 'https://Stackoverflow.com/users/777', 'pm_score': 0, 'selected': False, 'text': "<p>@aku, lomaxx and bdukes - your solutions are what I was looking for. </p>\n\n<p>@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. </p>\n\n<p>Serialize</p>\n\n<pre><code>    #'res is my object to serialize\n    Dim xml_serializer As System.Xml.Serialization.XmlSerializer\n    Dim string_writer As New System.IO.StringWriter()\n    xml_serializer = New System.Xml.Serialization.XmlSerializer(res.GetType)\n    xml_serializer.Serialize(string_writer, res)\n</code></pre>\n\n<p>Deserialize</p>\n\n<pre><code>    #'string_writer and xml_serializer from above\n    Dim serialization As String = string_writer.ToString\n    Dim string_reader As System.IO.StringReader\n    string_reader = New System.IO.StringReader(serialization)\n    Dim res2 As testsedie.EligibilityResponse\n    res2 = xml_serializer.Deserialize(string_reader)\n</code></pre>\n"}, {'answer_id': 41135, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>For what you are talking about, the BinaryFormatter suggested earlier will probably have the best performance.</p>\n'}, {'answer_id': 41165, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 0, 'selected': False, 'text': "<p>I'm backing @1800 Information on this one.<br>\nSerializing objects for long-term storage is never a good idea</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>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.</p>\n"}, {'answer_id': 41174, 'author': 'abigblackman', 'author_id': 2279, 'author_profile': 'https://Stackoverflow.com/users/2279', 'pm_score': 0, 'selected': False, 'text': '<p>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. </p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/40884', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/777/']