【发布时间】:2015-10-19 13:14:39
【问题描述】:
喂,
几个月前我在post 上发布了一个关于 CLR 用户定义聚合的问题。
这适用于 int。但现在我想用 datetime 参数做同样的功能。
但是我找不到工作。 像这样,代码将无法工作......问题在于在sql server上生成此错误的读取函数:
System.ArgumentOutOfRangeException: Les graduations doivent être comprises entre DateTime.MinValue.Ticks et DateTime.MaxValue.Ticks.
Nom du paramètre : ticks
System.ArgumentOutOfRangeException:
à System.DateTime..ctor(Int64 ticks)
à sMaxDatetime.Read(BinaryReader reader)
所以我尝试使用this 将我的sql Datetime 转换为刻度,但它不起作用。我在转换为日期时间时遇到了 OverflowException。
我找到了这个post,看来我无法将我的日期时间映射到 BinaryReader...
所以我没有什么想法来做我的聚合......
你有这个想法吗?
这是实际代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Collections;
using System.IO;
[Serializable]
[SqlUserDefinedAggregate(
Format.UserDefined,
IsInvariantToOrder = true,
IsInvariantToNulls = true,
IsInvariantToDuplicates = true,
MaxByteSize = -1)]
public struct sMaxDatetime : IBinarySerialize
{
#region Helpers
private struct MyData
{
public string Data { get; set; }
public DateTime? Group { get; set; }
public int CompareTo(MyData other)
{
if (Group == null)
return other.Group == null ? 0 : -1;
if (other.Group == null)
return 1;
return Group.Value.CompareTo(other.Group.Value);
}
public static bool operator < (MyData left, MyData right)
{
return left.CompareTo(right) == -1;
}
public static bool operator > (MyData left, MyData right)
{
return left.CompareTo(right) == 1;
}
}
#endregion
private MyData _maxItem;
public void Init()
{
_maxItem = default(MyData);
}
public void Accumulate(SqlString data, SqlDateTime group)
{
if (!data.IsNull && !group.IsNull)
{
var current = new MyData
{
Data = data.Value,
Group = group.Value,
};
if (current > _maxItem)
{
_maxItem = current;
}
}
}
public void Merge(sMaxDatetime other)
{
if (other._maxItem > _maxItem)
{
_maxItem = other._maxItem;
}
}
public SqlString Terminate()
{
return _maxItem.Data;
}
public void Read(BinaryReader reader)
{
//if (reader.ReadBoolean())
//{
_maxItem.Data = reader.ReadString();
_maxItem.Group = new DateTime(reader.ReadInt64());
//}
//else
//{
// _maxItem = default(MyData);
//}
}
public void Write(BinaryWriter writer)
{
if (_maxItem.Group.HasValue)
{
writer.Write(true);
writer.Write(_maxItem.Group.Value.ToString());
writer.Write(_maxItem.Data);
}
else
{
writer.Write(false);
}
}
}
PS : 我有这个相关的 post 未关闭的 sql_variant 可以解决问题,但我无法开始工作。
【问题讨论】:
-
你在哪一行得到这个异常?
标签: c# sql-server datetime clr