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

指定的转换无效
string StrSql = "select * from article order by submit_date desc";
  using (OleDbConnection Conn = new OleDbConnection(StrCon))
  {
  Conn.Open();
  OleDbCommand cmd = new OleDbCommand(StrSql, Conn);
  OleDbDataReader dr = cmd.ExecuteReader();
  List<article> list = new List<article>();
  while (dr.Read())
  {
  article Article = new article();
  for (int i = 0; i < dr.FieldCount; i++)
  {
  string fieldName = dr.GetName(i);
  if (fieldName == "arti_id")
  Article.Arti_id = (int)dr["arti_id"];
  if (fieldName == "user_id")
  {
  Article.User_id = (int)dr["user_id"];
  //Article.User_name = UserService.GetUserNameByUserId((int)dr["user_id"]).User_name;//未将对象引用设置到对象的实例
  }
  if (fieldName == "arti_title")
  Article.Arti_title = (string)dr["arti_title"];
  if (fieldName == "arti_text")
  Article.Arti_text = (string)dr["arti_title"];
  if (fieldName == "arti_click")
  Article.Arti_click = (int)dr["arti_click"];
  if (fieldName == "submit_date")
  Article.Submit_date = (DateTime)dr["submit_date"];//运行时此处报错“指定的转换无效”
  }
  list.Add(Article);
  }
  dr.Close();
  return list;
  }
大致知道是转换成access的日期时间类型时问题,求解此处如何能从C# 中转换成为access日期时间类型

------解决方案--------------------
//try one try
string str = dr["submit_date"].ToString();
DateTime = DateTime.Parse(str);

------解决方案--------------------

 //Article.User_name = UserService.GetUserNameByUserId((int)dr["user_id"]).User_name;//未将对象引用设置到对象的实例

====》

可能是dr["user_id"]没取到值或者UserService.GetUserNameByUserId((int)dr["user_id"])没得到值。。。


-----------------------

Article.Submit_date = (DateTime)dr["submit_date"];//运行时此处报错“指定的转换无效”


====》

F5.。。看看dr["submit_date"]得到的是个什么
------解决方案--------------------
Article.Submit_date = (DateTime)dr["submit_date"];/
可能为空,建议不要强制转换