直接序列化的C#类到SQL Server?直接、序列化、Server、SQL

2023-09-02 20:45:45 作者:╭ァ再回首ヾ恍然如夢

任何人都可以提出来序列数据的最佳方式(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.

 
精彩推荐
图片推荐