我不断收到错误System.InvalidCastException:指定的转换无效。在运行期间。 RewardJoinType在数据库中可以为null。可空的Enum转换为Int16
这是代码行转换失败:
c.rewardJoinType = (RewardJoinType)reader.GetInt16();
'reader.GetInt16()' 投掷类型的异常则 '' 短{System.InvalidCastException}
在一个类中我有下面的代码行:
private RewardJoinType? rewardJoinType;
...一些其他的代码
c.rewardJoinType = (RewardJoinType?)reader.GetInt16();
...一些其他的代码
conn.AddParam("@rewardJoinType", (int?)rewardJoinType);
...一些其他的代码
public RewardJoinType? RewardJoinType
{
get { return rewardJoinType; }
set { rewardJoinType = value; }
}
而这里的枚举本身
public enum RewardJoinType
{
Auto,
Manual
}
是因为通过默认枚举是Int32甚至t hough我有它可以为空它不能够投出一个空Int16?
我们处理的DBNull为Int16的,像这样已经在我们的读者:
public short GetInt16()
{
columnIndex++;
return reader.IsDBNull(columnIndex) ? (short)0 : reader.GetInt16(columnIndex);
}
InvalidCastException在哪里被抛出?哪一行代码?以及它试图从和来自哪里? – thecoop 2009-10-14 13:43:28
已更新。它只会在异常中表示无效投射。我没有得到内心的例外。往上看。 – PositiveGuy 2009-10-14 13:46:22
啊,我已经把这个领域作为DB中的TinyInt。 – PositiveGuy 2009-10-14 13:51:48