我在C#中使用日期编写了一个程序。它从SQL Server表中获取值。我使用的是Windows 7并且程序运行良好。我有这行代码:无法将DateTime从SQL Server转换为C#日期时间
DateTime fechaOperacion = Convert.ToDateTime(reader["FechaOperacion"]);
读者在24小时格式返回的日期,我可以将其转换成一个DateTime
变量。
现在,我做了一个系统升级到Windows 10的代码,同一行抛出以下错误:
String was not recognized as a valid DateTime. there is an unknown word starting at index 20.
而且现在的读者上午返回/下午格式,并在索引20有a.m或p.m.
我曾尝试以下的事情:
改写的代码行:
Convert.ToDateTime(reader["FechaOperacion"], System.Globalization.CultureInfo.InvariantCulture)
reader.GetDateTime(reader.GetOrdinal("FechaOperacion"));
转换的文化,24小时格式
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(1033);
但这一切,似乎工作,我不知道自己还能做些什么。
定义“它似乎不工作”的转化。尤其是,'reader.GetDateTime(reader.GetOrdinal(“FechaOperacion”))'的结果是什么,这是正确读取独立于任何格式的DATETIME列的方法? –
究竟**是什么**阅读器[“FechaOperacion”]'返回?它提到'String'的事实意味着它不是“sql DateTime” - SQL'datetime'值可以很好,不需要解析。那么,它是什么? –
如果'FechaOperacion'是一个DATETIME列,那么它听起来像是一个显示问题。如果你做了'var dateStr = fechaOperacion.ToString(“dd/MM/yyyy HH:mm:ss”);'? 'dateStr'的价值是什么? – Howwie