直接序列化的C#类到SQL Server? [英] Serialize C# class directly to SQL server?

查看:352
本文介绍了直接序列化的C#类到SQL Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提出来序列数据的最佳方式(A类实际上)到一个数据库?

can anyone suggest the best way to serialize data (a class actually) to a DB?

我使用SQL Server 2008,但我presume我需要在数据库中存储之前的类将字符串/或其他数据类型的序列化?

I am using SQL server 2008 but i presume i need to serialize the class to a string / or other data type before storing in the database?

我presume此字段必须是文本或二进制??

I presume that this field needs to be text or binary??

请问SQL Server 2008中(或.NET 3.5)支持直接序列tothe数据库??

Does SQL server 2008 (or .net 3.5) support serializing directly tothe database ??

任何帮助真的AP preciated

Any help really appreciated

推荐答案

您可以XML中的类序列化到XML字段。我们使用一个ETL这一切的时候了异常日志记录。

You can xml serialize the class into an xml field. We use this all the time for exception logging in an ETL.

使用XmlSerializer你可能需要一个辅助方法,其中的某处序列化类的字符串...

Using the XmlSerializer you may want a helper method somewhere which serializes the class to a string...

public static string SerializeToXml<T>(T value)
{
    StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    serializer.Serialize(writer, value);
    return writer.ToString();
}

然后,只需把字符串转换成像任何其他数据库。

Then just put the string into the db like any other.

这篇关于直接序列化的C#类到SQL Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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