c# – 从SQL数据库中读取日期时间值

如何从SQL数据库中读取日期时间字段?
这不起作用:
emp.DateFacturation = (DateTime)(dr["DateFacturation"])
这也不是:
emp.DateFacturation = DateTime.Parse(dr["DateFacturation"].toString());
解决方法:
由于您已经评论了一个现已删除的答案:
dr is a DataReader
您可以使用SqlDataReader.GetDateTime.如果值为DBNull,则应首先检查DataReader.IsDBNull:
DateTime dateFacturation;
int colIndex = dr.GetOrdinal("DateFacturation");
if(!dr.IsDBNull( colIndex ))
dateFacturation = dr.GetDateTime( colIndex );
我使用GetOrdinal获取列名的列索引以将其传递给GetDateTime.