日期:2014-05-18 浏览次数:20910 次
public List<BusResordModel> GetAll() { try { List<BusResordModel> list = new List<BusResordModel>(); using (SqlDataReader dr = sqlH.ExecuteReader("GetAllBusRecord", CommandType.StoredProcedure, null)) { while (dr.Read()) { BusResordModel busM = new BusResordModel(); busM.CardNo = dr["cardNo"].ToString(); busM.UserName = dr["userName"].ToString(); busM.BusNo = dr["busNo"].ToString(); busM.BusType = dr["busType"].ToString(); try { busM.InTime = Convert.ToDateTime(dr["inTime"]); if (busM.InTime==null) { busM.InTime = Convert.ToDateTime(""); } } catch (Exception) { } busM.InAddr = dr["inAddr"].ToString(); try { [color=#0000FF]busM.OutTime = Convert.ToDateTime(dr["outTime"]);[/color] if (busM.OutTime ==Convert.ToDateTime("0001-1-1 0:00:00")) { busM.OutTime = Convert.ToDateTime(""); } } catch (Exception) { } busM.OutAddr = dr["outAddr"].ToString(); busM.TotalTime = Convert.ToInt32(dr["totalTime"]); busM.Price = dr["price"].ToString(); busM.Operater = dr["operater"].ToString(); busM.Picture = dr["picture"].ToString(); busM.Photo = dr["photo"].ToString(); busM.Guid = dr["guid"].ToString(); list.Add(busM); } return list; } } catch { return null; } }
if(dr["outTime"]!=null) { busM.OutTime=Convert.ToDateTime(dr["outTime"]); } else { busM.OutTime=Convert.ToDateTime("2010-01-01");//设置个默认值啊 }
------解决方案--------------------
//有些数据库字段取出来,只判断是不是null是没用的,有可能是DBNull类型
if(dr["inTime"]==null || dr["inTime"]!==DBNull.Value)
{
//如果空就赋默认值
busM.InTime =DateTime.Now;
}
else
{
try
{
busM.InTime =Convert.ToDateTime(dr["inTime"]);
}
catch (Exception)
{
//如果错误就赋默认值
busM.InTime =DateTime.Now;
}
}
------解决方案--------------------
class BusResordModel{ privaite DateTime? _outTime = null; public DateTime? outTime { get{....} set{....} } //使用? 表示此属性允许null值。在程序中可判断: if(outTime.HasValue) outTime.Value..... ..... } ....... object obj = dr["outTime"]; if(obj != null && obj != DBNull.Value) ....outTim