【问题标题】:How to Serialize an Object from C# to a mySql database如何将对象从 C# 序列化到 mySql 数据库
【发布时间】:2015-01-11 19:41:07
【问题描述】:

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

【问题讨论】:

标签: c# mysql serialization


【解决方案1】:

你可以使用反射:

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 属性应该返回一个已经打开的数据库连接,或者您可以插入一个每次都打开它的代码。

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

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

【讨论】:

  • 非常不相关(并且令人困惑),因为问题只是关于基本序列化。
猜你喜欢
  • 2017-07-03
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 2010-10-09
  • 2018-10-11
相关资源
最近更新 更多