如何将对象从C#序列化到mySql数据库

时间:2015-01-11 19:41:08

标签: c# mysql serialization

我是C#的新手我想创建一个数据库,我可以存储一个序列化对象,然后检索它们并转换为相关对象类型

我的数据库是mysql,并且有一个BLOB类型来存储序列化数据

我不希望使用XML序列化 我想在java

中使用纯C#序列化它

如果有人可以给我一个链接或一些帮助,那就太棒了...... !!!

这是我要序列化的课程

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用反射:

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;
    }

缺少方法:

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;
    }

Connection属性应返回已打开的数据库连接,或者您可以插入每次打开它的代码。

注意: 对象的属性需要与表的列名完全对应。

我希望这对你有所帮助,因为这对我有很多帮助..