Re: Serializing/Deserializing to Database
- From: "Greg Young" <druckdruckREMOVEgoose@xxxxxxxxxxx>
- Date: Tue, 30 May 2006 18:33:19 -0400
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
.
- Follow-Ups:
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- References:
- Re: Serializing/Deserializing to Database
- From: Greg Young
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- From: Alex Maghen
- Re: Serializing/Deserializing to Database
- Prev by Date: Re: Serializing/Deserializing to Database
- Next by Date: Re: Basic Object Serialization to a STRING
- Previous by thread: Re: Serializing/Deserializing to Database
- Next by thread: Re: Serializing/Deserializing to Database
- Index(es):
Relevant Pages
|