我提供了包含整数类型列的记录,而不是报告错误(记录为here)在下面的方法中得到InvalidCastException
(用于填充存储中的记录):
protected void FillRecordOrder(object rec, object[] fields)
{
OrdersVerticalBar record = (OrdersVerticalBar) rec;
record.OrderDate = (DateTime) fields[0];
}
如何在Filehelpers库中使用SqlStorage处理错误?
答案 0 :(得分:1)
字段[0]的内容是什么?你是说它包含整数?然后你需要将它以某种方式转换为DateTime。类似的东西:
protected void FillRecordOrder(object rec, object[] fields)
{
OrdersVerticalBar record = (OrdersVerticalBar) rec;
if (fields[0] == null)
record.OrderDate = DateTime.MinValue;
else if (fields[0] is DateTime)
record.OrderDate = (DateTime)fields[0];
else if (fields[0] is int)
{
DateTime baseDate = new DateTime(1900, 1, 1);
DateTime newDate = baseDate.AddDays((int)fields[0]);
record.OrderDate = newDate;
}
}