日期:2014-05-18  浏览次数:20784 次

请教:从数据库中取出的空值(Datetime型),但在CS代码中报错,转换无效!
想从数据库中取值,但有可能这个字段(类型为Datetime)中的值是空的 <Null> ,
      CS代码中
if(Convert.IsDBNull(object))
{
}
判断,但还是出错:指定的转换无效

请教如何解决?

------解决方案--------------------
你怎么取得值?

例如 用Reader
if (reader[0] != DBNull.Value)
{
dmoney = reader.GetDecimal(0);

}
else dmoney = 0;

------解决方案--------------------
你debug一下吧,应该是if(product.EndDate.ToString() != string.Empty)
这句有问题吧,如果product.EndDate是null的话,就会出错了
------解决方案--------------------
把product.StartDate和product.EndDate都改成字符型的, 在取数据那一层就判断, 如果为空, 就product.StartDate=string.Empty; product.EndDate=string.Empty