Posted By: Shmuelpro | Nov 4th, 2006 @ 9:32 AM
page 1 of 1
Comments: 4 | Views: 4459
I am trying to store a class in Sql server 2005.
I succeded by serializing it and storing it in a Binary column, the problem with that is, it is extreamly redundent to be store it on the drive and than reload it just to be stored into the database.

Is there a more efficient way to go about this?
I'm guessing that you are using XmlSerializer to serialize the class. You could a MemoryStream as the stream where to store the XML to. You could use that MemoryStream then to store the bytes to the database Smiley

I hope this helps.

No i am serializing it using the Binary formatter.
I might not have explained myself correctly.
what i am basicly trying to do is turn my class into a series of bytes inorder for them to be stored in the database.

The Memory stream NEEDS the bytes so i am kinda in a pickle.

Thanks for the quick reply

You should post a little bit of your code Smiley Makes it easier to understand what you actually trying to do.
I tried this and it worked without problems.

[Serializable]
public class Foo
{
    private int _prop1;
    private string _prop2;

    public string Prop2
    {
        get { return _prop2; }
        set { _prop2 = value; }
    }

    public int Prop1
    {
        get { return _prop1; }
        set { _prop1 = value; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Foo f = new Foo();
        f.Prop1 = 1;
        f.Prop2 = "test";

        MemoryStream stream = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, f);

        // Holds the result of the serialization.
        byte[] bytes = stream.ToArray();

        // Deserialize the bytes array.
        f = (Foo)formatter.Deserialize(new MemoryStream(bytes));
    }
}

page 1 of 1
Comments: 4 | Views: 4459