SQL Server CLR Aggregate: Serialize a Dictionary?

孤街浪徒 提交于 2019-12-12 02:33:10

问题


I'm writing a custom CLR Aggregate in C# to run in SQL Server 2008. I want to hold most of my SqlUserDefinedAggregate instances' states in Dictionaries, so I can't use the builtin serialization.

How can I go about using UserDefined serialization with the Read() and Write() methods?


回答1:


Using keys and values of types that BinaryReader and BinaryWriter handle, Write each Dictionary out to the BinaryWriter in key0,value0...keyN-1,valueN-1 order. Prefix this sequence with Dictionary's Count. After the sequence, write a constant to verify end of dictionary. Personally I like 0xDEADBEEF.

For deserialization, do the reverse: Intialize a loop to run n times (n is the first integer in your stream), and add to Dictionary as you read in key/value pairs. At the end, check for your constant.



来源:https://stackoverflow.com/questions/8901233/sql-server-clr-aggregate-serialize-a-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!