Can I save an 'Object' in a SQL Server database?

后端 未结 5 419
傲寒
傲寒 2020-12-04 10:29

I want to save an object (of any type) into a field in a database in SQL Server 2005. Is it possible? Do I have to convert the object into something, like a byte array for e

5条回答
  •  隐瞒了意图╮
    2020-12-04 10:44

    As others have said, serialization may be the key here (assuming you don't want to use ORM to store the properties as columns in a table, which seems much more direct).

    Some caveats though; a database is:

    • long term storage
    • not related to your .NET code

    As such, you do not want to use any serialization technique that is platform-specific or version-specific. You will often see people mention BinaryFormatter for persistance, but this falls into both of the above traps. You would be scuppered if you ever changed platform, or even if you just change some properties.

    You need an implementation-independent approach; the simplest (which also retains the ability to be human readable) is xml or json, perhaps via XmlSerializer or Json.NET (stored in a [n]varchar(max)). If you don't care about human readable, "protocol buffers" (fast/binary) would do well (stored in a varbinary(max)), and is available for most platforms (including C#/.NET/etc).

提交回复
热议问题