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

Med*_*ine 5 c# sql datetime

如何从SQL数据库中读取日期时间字段?

这不起作用:

emp.DateFacturation = (DateTime)(dr["DateFacturation"])
Run Code Online (Sandbox Code Playgroud)

这也不是:

emp.DateFacturation = DateTime.Parse(dr["DateFacturation"].toString());
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 6

由于您已经评论了一个现已删除的答案:

dr是一个DataReader

你可以用SqlDataReader.GetDateTime.您应首先检查值是否DBNullDataReader.IsDBNull:

DateTime dateFacturation;
int colIndex = dr.GetOrdinal("DateFacturation");
if(!dr.IsDBNull( colIndex ))
    dateFacturation = dr.GetDateTime( colIndex );
Run Code Online (Sandbox Code Playgroud)

GetOrdinal用来获取列名的列索引来传递给它GetDateTime.