如何序列化对象并将其存储到数据库 [英] How to serialize object and store to database

查看:433
本文介绍了如何序列化对象并将其存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发业务应用程序.我有一个名为Item的主类,还有一些派生类.我想序列化并将这些对象保存到数据库.我正在使用C#Visual Studio 2010和MS SQL 2008 R2.
什么是最佳做法?我可以使用LINQ to SQL吗?
该过程应尽可能快,因为它将同时在10个网络客户端和5个通过RDP的客户端上工作.

I''m developing business application. I have main class called Item and a few derived classes. I want to serialize and save these objects to database. I’m using C# Visual Studio 2010 and MS SQL 2008 R2.
What is a best practice? Can I use LINQ to SQL?
The process should be as fast as possible, because it is going to work on 10 network clients and 5 clients over RDP at the same time.

Item2 i1 = new Item2() { Width = 500, Height = 100 };
Item3 i2 = new Item3() { Color = Color.Black };
class Item
{
    public string Name { get; set; }
    public float Price { get; set; }
    public virtual void Draw(Graphics g)
    {
    }
}
class Item2 : Item
{
    public double Width { get; set; }
    public double Height { get; set; }
}
class Item3 : Item
{
    public Color Color { get; set; }
}

推荐答案

您可以使用XmlSerializer
http://msdn.microsoft.com/en-us/library/system. xml.serialization.xmlserializer.aspx
You can use the XmlSerializer class
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx


This good [^]article (with code samples) could help you out.


我已经使用过BinaryFormatter和varbinary (max)在sql中,与LINQ一起使用时效果很好.它比XmlSerializer快得多,可以自动检测派生类并支持更多类型.
I have used BinaryFormatter and varbinary(max) in sql, with LINQ and works great. It''s much faster than XmlSerializer, automatically detects derived classes and supports more types.


这篇关于如何序列化对象并将其存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆