user3363357
user3363357

Reputation: 99

How to Serialize an Object from C# to a mySql database

Im fairly new to C# i want to create a database where i can store a serialized object and then retrieve them and cast to the relavant object type

My database is mysql and has a type BLOB to store the serialized data

I do not want tu use XML serialization i want to serialize this using pure C# like in java

if someone can give me a link or some help doing this it would be great...!!!

This is the class i want to serialize

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Login 
{
    [Serializable()]
    class Worker : ISerializable
    {

        String fName;

        public String FName
        {
            get { return fName; }
            set { fName = value; }
        }

        String lName;

        public String LName
        {
            get { return lName; }
            set { lName = value; }
        }

        String TP;

        public String TP1
        {
            get { return TP; }
            set { TP = value; }
        }

        String department;

        public String Department
        {
            get { return department; }
            set { department = value; }
        }

        public Worker(String fname,String lname , String tp , String Departhment )
        {
            this.fName = fname;
            this.lName = lname;
            this.TP = tp;
            this.Department = department;
        }


        public void getObjectData(SerializationInfo info , StreamingContext context)
        {
            info.AddValue("fName",fName);
            info.AddValue("lName", lName);
            info.AddValue("TP",TP);
            info.AddValue("Department", Department);
        }
    }
}

Upvotes: 1

Views: 2331

Answers (1)

syntax error
syntax error

Reputation: 149

You can use reflection:

public T GetEntity<T>(DbCommand command)
    {
        var instance = Activator.CreateInstance(typeof(T));

        var properties = typeof(T).GetProperties();
        using (var connection = Connection)
        {
            command.Connection = connection;

            using (IDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    foreach (var property in properties.Where(property => property.CanWrite)
                        .Where(p => PropertyIsReadable(reader, p.Name)))
                    {
                        if (property.PropertyType == typeof(double))
                        {
                            property.SetValue(instance, reader.GetDouble(reader.GetOrdinal(property.Name)), null);
                            continue;
                        }

                        property.SetValue(instance, reader[property.Name], null);
                    }
                }
            }

        }

        return (T)instance;
    }

And the missing method:

private bool PropertyIsReadable(IDataReader reader, string propertyName)
    {
        var result = false;

        for (var i = 0; i < reader.FieldCount; i++)
        {
            if (!reader.GetName(i).Equals(propertyName, StringComparison.InvariantCultureIgnoreCase))
                continue;

            result = !reader[propertyName].Equals(DBNull.Value);

            break;
        }

        return result;
    }

The Connection property should return a database connection that is already open or you can insert a code that opens it everytime.

NOTE: The properties of your object needs to correspond exactly as the column names of your table.

I hope this helps you as this helps me a lot..

Upvotes: 1

Related Questions