5

we can easily serialize class to flat file but if i want to serialize class to database then how could i do it and if it is possible then how can i deserialize data from db to class. please give a sample code in c#.

thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 1
    you can serialize/de-serialize to any stream - essentially what you get is bunch of bytes - that you can save in file or database (use varbinary column in Sql Server, blob in oracle). – VinayC Jan 19 '11 at 08:40
  • 1
    You're better of creating a decent database layout and converting that into C# code then the other way around. Use a decent OR mapper or just create type datasets/linq2sql. – CodingBarfield Jan 19 '11 at 08:46

6 Answers6

8

Like other said at already, you can use binary serialization to get the Byte[] array in a memory stream and then create a column in database of type Blob / image to store the the byte array.

Then while reading back, you just read the value of the column in the stream back by using technique called deserialization

Serialization

  BinaryFormatter bf = new BinaryFormatter();

        List<SearchFilterDetails> _list = QueryFilterDetails.ToList<SearchFilterDetails>();

        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, _list);
            return ms.GetBuffer();
        }

Deserialization

  private void DeSerilizeQueryFilters(byte[] items)
    {
        BinaryFormatter bf = new BinaryFormatter();

        List<SearchFilterDetails> _list = new List<SearchFilterDetails>();

        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(items, 0, items.Length);
                ms.Position = 0;

                _list = bf.Deserialize(ms) as List<SearchFilterDetails>;
            }

            foreach (SearchFilterDetails mobj in _list)
            {
                QueryFilterDetails.Add(mobj);
            }
        }
        catch (Exception ex)
        {
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
2

Thomas, what kind of data access logic are you using in your application?

If you are looking for a way to interact with the database and store/retrieve objects from it, you could also have a look at Entity Framework or NHibernate, such ORMs allow you to focus on what really matters in your application and not on the way objects/entities are loaded or saved.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

Using binary serialization to BLOB column, or using XML\Data contact\net Data contact to XML column.

Edit: Binary example already given by @Saurabh. Data contract example provided by Peter Ritchie's blog.

HuBeZa
  • 4,715
  • 3
  • 36
  • 58
1

When serializing an object, it is often done to a stream. You can serialize to a memory stream, then read the bytes back and put them into a database.

What serialization code do you have currently? It would be better to tell you how to modify it than to just give you the full code.

cjk
  • 45,739
  • 9
  • 81
  • 112
1

You can serialize a class (out of the box) to binary form or to XML.
In the first case you get a byte[], and in the second you get a simple string.
If you want to persist that in the database, just treat it as any other byte array or string (or XML, if your database natively supports that) that need to be sent to the database.

SWeko
  • 30,434
  • 10
  • 71
  • 106
1

Serialize an object into XML. Try and let a dataset read from that XML blob. Create an DB create statement for each table/row in the dataset.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54