我正在尝试创建一个 SqlUserDefinedAggregate
在 C# 中附加到我的 SQL Server 2008 实例。我正在使用 .NET 3.5。基本上,我想计算我看到字符串值的次数。由于使用,它确实需要是一个聚合函数。该函数的代码在逻辑上是合理的,但是当我去部署时,我得到了这个:
Deploy error SQL01268: .Net SqlClient Data Provider: Msg 6222, Level 16, State 1, Line 1 Type "GEMCLR.CountTypes" is marked for native serialization, but field "m_types" of type "GEMCLR.CountTypes" is not valid for native serialization.
m_types
是 Dictionary<string, int>
.我的代码大纲如下所示:
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate (Format.Native)]
public struct CountTypes
{
private Dictionary<string, int> m_types;
public void Init ()
{
m_types = new Dictionary<string, int> ();
}
public void Accumulate (SqlString value) { ... }
public void Merge (CountTypes group) { ... }
public SqlString Terminate () { ... }
}
最佳答案
通常字典不是开箱即用的可序列化,这是你的问题,有很多文章介绍如何解决这个问题,例如 Adam Semel 的 How to Serialize a Dictionary or Hashtable in C# .
关于C# 和 SQL Server 2008 CLR 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7270785/