C #: How can you store arbitrary objects in SQL Server?

Suppose you have various objects of an arbitrary type that you would like to store in the type + table type value. The key can be, for example, int, string or guid. What would be the value? String, binary or something else?

And how will you store and load objects? I would think of some kind of serialization, but which one?


I have one solution at a time when I have a class with these two methods:

public T Get<T>(string key)

public void Set<T>(string key, T value)

In the database, I have a table with a row column and a binary column. Then I use BinaryFormatterfor serialization and deserialization the values ​​and Linq2Sql to put the binary result in the database table. But is this a good solution? Currently, I just dared to try this with simple values ​​like integers and strings. How do BinaryFormatterserialization generally work with more complex types like structs and class? Especially if, for example, the value contains things such as arrays or lists.

Any pointers?


, . . , . , , .

+3
3

, SQL, , , . , , ad-hoc. , .NET.

. , BinaryFormatter XmlSerializer; XML , . , .

, Thrift Protocol . , ), . ( : 20% - - # , .)

- , , /? , "" , , . , , "" - , , .

. , - , , .

+6

, # varbinary (max) SQL Server.

EDIT

, .

+1

.

Eg.

  • Guid: UNIQUEIDENTIFIER
  • , int, decimal, byte ..: decimal
  • string: NVARCHAR
  • DateTime: DateTime

NHibernate, , . :

create table dbo.Value (
  Name NVARCHAR(80) not null,
  Type NVARCHAR(6) not null,
  TextValue NVARCHAR(500) null,
  GuidValue UNIQUEIDENTIFIER null,
  NumericValue DECIMAL(36, 18) null,
  DateTimeValue DATETIME null
)

- "", "GUID", "NUMBER" "DATE". NHibernate, .

, , NH any ( ).

0

Source: https://habr.com/ru/post/1712441/


All Articles