Re: Serializing/Deserializing to Database

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Use a MemoryStream
http://www.dotgnu.org/pnetlib-doc/System/IO/MemoryStream.html instead of a
FileStream it has a getbytes() method that will give you the relevant bytes.

For the serializable attribute .. it is just [Serializable]

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung


"Alex Maghen" <AlexMaghen@xxxxxxxxxxxxxxxx> wrote in message
news:8FA5A6E7-291B-4444-B83D-7DB1A4823399@xxxxxxxxxxxxxxxx
One more little thing: I'm a little confused about how I get my hands on
the
string that represents that serialized class so that I can pass it along
to
write to the database.

If I do...
Stream stream = File.Open("data.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();

formatter.Serialize(stream, obj);

I can wite it to a file. But how do I just get it into a string instead?

Alex



"Alex Maghen" wrote:

Thanks for this. Can youhelp me with the specific syntax for marking the
class "Serializable"?

Alex

"Greg Young" wrote:

1) It should be tagged as Serializable
2) You could either use binary or XML serialization.

If you use binary use Convert.ToBase64 before inserting it to your
database
and COnvert.FromBase64 when you have read the object. This step is not
required with XML

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"Alex Maghen" <AlexMaghen@xxxxxxxxxxxxxxxx> wrote in message
news:C10EBF12-F50C-444D-B533-5899FEA4A418@xxxxxxxxxxxxxxxx
I have a class, "MySavableClass." The class has one string property
named
"MyID" and then a bunch of other properties.

Ideally, what I'd like to do is write a "Save()" and a "Restore()"
method -
or probably static methods for this object that would work as
follows:

private MySavableClass MyC = new MySavableClass();
MyC.MyID = "Class1";
MyC.SomeOtherProperty = 1234;

//The next line will cause the class "MyC" to be serialized and then
written
//to a two column database tabe. Column 1 will be the ID column
which,
//in this case, will be "Class1", and column 2 will be a Text/Memo
column
//which will take the entire serialized object.
Success = MySavableClass.Save(MyC);

//Later, I should be able to do this...

private MySavableClass MyCRestored;
MyCRestored = MySavableClass.Restore("Class1");
if(MyCRestored == null)
//Restoration failed!
else
Writeln("MyCRestored.SomeOtherProperty = " +
MyCRestored.SomeOtherProperty.ToString());

etc., etc.

All of this doesn't seem that hard but I have a few questions:

1. Do I have to set up any meta-tagging on my calss to make it
serializable
(it's a pretty simple class.

2. Which serialization method should I use if I want to save the data
as
text to a database column?

Any other guidance you can provide will be much appreciated.

Alex






.



Relevant Pages

  • Re: Serializing/Deserializing to Database
    ... MVP - C# ... "Greg Young" wrote: ... write to the database. ... You could either use binary or XML serialization. ...
    (microsoft.public.dotnet.framework)
  • Re: Why would I want to serialise something
    ... >I read a lot about serialising things to XML. ... Serialization is a very helpful mechanism. ... rows and columns and stored in a relational database. ...
    (microsoft.public.dotnet.general)
  • Re: ASP.Net Caching Questions
    ... A serialization option you might consider is binary ... database will perform comparably to using Cache. ... large amounts of static data into memory and do fast lookups on it. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: question about serialization
    ... I was wondering for what cases would serialization be ... Use case 1) is where relational databases where invented for and can by no means be compared to object serialization in Java. ... In this situation the database is one of the core deliverables as well as the integration point where custom applications and generic analysis and reporting tools, backup tools etc. can be linked together. ...
    (comp.lang.java.programmer)
  • Re: Serializing/Deserializing to Database
    ... "Greg Young" wrote: ... You could either use binary or XML serialization. ... If you use binary use Convert.ToBase64 before inserting it to your database ...
    (microsoft.public.dotnet.framework)