我创建了IBinarySerialize
区域,以便为我的用户定义的聚合创建我的 CLR。 我正在尝试在 C# 中复制我的 XIRR 函数,只是为了学习如何使用 CLR。 我以相同的顺序输入和读取,但是当我尝试构建时,出现错误:
严重性代码说明 项目文件行抑制状态
错误 CS1503 参数 1:无法从 'System.Data.SqlTypes.SqlDateTime' to 'bool' CustomAggregates D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs 255 Active
错误来自写入部分。
在我看到的一些例子之后,我似乎找不到我在这种安排中缺少的东西。 这是结构的获取/设置
[Serializable]
[SqlUserDefinedAggregate(
Format.Native,
IsInvariantToDuplicates = false, // Receiving the same value again changes the result
IsInvariantToNulls = false, // Receiving a NULL value changes the result
IsInvariantToOrder = true, // The order of the values affects the result
IsNullIfEmpty = true, // If no values are given the result is null
MaxByteSize = -1, // Maximum size of the aggregate instance. -1 represents a value larger than 8000 bytes, up to 2 gigabytes
Name = "XIRR" // Name of the aggregate
)]
public struct XIRR : IBinarySerialize
{
/// <summary>
/// Used to store the product
/// </summary>
public SqlDouble Result { get; private set; }
public SqlDouble Cashflow { get; private set; }
public SqlDateTime CashflowDateTime { get; private set; }
public bool HasValue { get; private set; }
public List<CashItem> CashFlows { get; private set;}
...
#region IBinarySerialize
/// <summary>
/// Writes the values to the stream in order to be stored
/// </summary>
/// <param name="write">The BinaryWriter stream</param>
public void Write(System.IO.BinaryWriter write)
{
write.Write(Result);
write.Write(Cashflow); //Line - 255
write.Write(CashflowDateTime);
write.Write(HasValue);
}
/// <summary>
/// Reads the values from the stream
/// </summary>
/// <param name="read">The BinaryReader stream</param>
public void Read(System.IO.BinaryReader read)
{
Result = new SqlDouble(read.ReadDouble());
Cashflow = new SqlDouble(read.ReadDouble());
CashflowDateTime = new SqlDateTime(Convert.ToDateTime(read.ReadString()));
HasValue = read.ReadBoolean();
}
#endregion IBinarySerialize
}
提前谢谢。
如果您希望我提供更多信息,请告诉我。
问题很可能是您的类型不匹配。Result
、Cashflow
和CashflowDateTime
的确切类型是什么?根据Read
方法,它们是Sql*
类型。他们真的是这样宣布的吗?还是宣布它们Double
和DateTime
?
至少我认为您在两个方向上都不正确地处理了CashflowDateTime
。假设CashflowDateTime
确实是一个SqlDateTime
,那么我猜您可能需要在Write
方法中替换此行:
write.Write(CashflowDateTime);
具有以下两行:
write.Write(CashflowDateTime.DayTicks);
write.Write(CashflowDateTime.TimeTicks);
然后,替换Read
方法中的此行:
CashflowDateTime = new SqlDateTime(Convert.ToDateTime(read.ReadString()));
具有以下内容(从"DayTicks"和"TimeTicks"值重建SqlDateTime
(:
CashflowDateTime = new SqlDateTime(read.ReadInt32(), read.ReadInt32());
也:
#region IBinarySerialize
在这里没有任何功能。删除它不会影响代码。
有关使用 SQLCLR- 的详细信息,请参阅: SQLCLR 信息