简体   繁体   English

System.FormatException: 'String '"2022-04-14 13:03:12"' 未被识别为有效的 DateTime。'

[英]System.FormatException: 'String '"2022-04-14 13:03:12"' was not recognized as a valid DateTime.'

cnn.Open();
using (StreamReader reader = new StreamReader(@"C:/Users/jhogan/Documents/Test Folder/test_import.csv"))
                {
                  while (!reader.EndOfStream)
                  {
                      var line = reader.ReadLine();
                      if (lineNumber != 0)
                      {
                         var values = line.Split(',');
                         var date = values[0];
                         var sentDate = DateTime.ParseExact(date, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
                    
                         await cnn.ExecuteAsync(insertFeedback, new { 
       @SentDate = sentDate, @RatedDate = values[1], @Rating = values[2], @Location = values[3], @Notes = values[4], @Email = values[5], @Feedback = values[6] },commandTimeout: 120,commandType: CommandType.StoredProcedure);}

I am attempting to load this data from a csv file into my database, but am running into the following error from the title.我试图将此数据从 csv 文件加载到我的数据库中,但遇到标题中的以下错误。 The original formatting of the date from my csv is HH:MM:SS PM/AM and my column type in the database is datetime.我的 csv 中日期的原始格式是 HH:MM:SS PM/AM,我在数据库中的列类型是日期时间。 In my database, the datetime formatting is YYYY-MM-DD HH:MM:SS:MMM.在我的数据库中,日期时间格式为 YYYY-MM-DD HH:MM:SS:MMM。 I think this is because of the database's formatting of things, but I am genuinely not sure.我认为这是因为数据库的格式问题,但我真的不确定。

my column type in the database is datetime.我在数据库中的列类型是日期时间。 In my database, the datetime formatting is YYYY-MM-DD HH:MM:SS:MMM.在我的数据库中,日期时间格式为 YYYY-MM-DD HH:MM:SS:MMM。

You misunderstand how the database and.Net DateTime values work.您误解了数据库和 .Net DateTime 值的工作方式。 If the column type or data type is DateTime , the format in the database or in memory is binary , and when you see YYYY-MM-DD HH:MM:SS:MMM that's just a convenience provided for you by your tooling.如果列类型或数据类型为DateTime ,则数据库或 memory 中的格式为binary ,当您看到YYYY-MM-DD HH:MM:SS:MMM时,这只是您的工具为您提供的便利 There is no need to ever try to match your string to that internal binary format.无需尝试将您的字符串与该内部二进制格式匹配。

But as for the code, the problem is the lower-case hh in the format string.但是至于代码,问题是格式字符串中的小写hh We can see in the documentation the lower-case h is for 12-hour times and upper-case H is for 24 hour times.我们可以在文档中看到小写的h是 12 小时制,大写的H是 24 小时制。 Looking at the input string 2022-04-14 13:03:12 from the error in the question title, we have a 13 in this position, which definitely puts you in upper-case H territory:从问题标题的错误中查看输入字符串2022-04-14 13:03:12 ,我们在这个 position 中有一个13 ,这肯定会让您处于大写H区域:

var sentDate = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

But it's worse than that.但它比那更糟。 I also see this in a comment:我也在评论中看到了这一点:

the first row is 4/14/2022 1:03:12 PM第一行是 4/14/2022 1:03:12 PM

So first of all, the format string for DateTime.ParseExact() tells the format of the original input source string, not the target format for the parsed value , which is, after all, binary and not human readable anyway.所以首先, DateTime.ParseExact()的格式字符串告诉原始输入源字符串的格式,而不是解析值的目标格式,毕竟它是二进制的,无论如何都不是人类可读的。

The format string to parse that value would look like this: M/dd/yyyy h:mm:ss tt .解析该值的格式字符串如下所示: M/dd/yyyy h:mm:ss tt Also note the use of paired vs single characters in certain positions.还要注意在某些位置使用成对字符和单个字符。

var sentDate = DateTime.ParseExact(date, "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

But the real significance here is the variation between the error in the question title and the comment means you might have a mix of formats.但这里真正重要的是问题标题中的错误和评论之间的差异意味着您可能会混合使用多种格式。 That's scary.太可怕了。 DateTime.ParseExact() uses strict parsing. DateTime.ParseExact()使用严格解析。 If the input does not EXACTLY match the expected format you WILL get an exception.如果输入与预期格式不完全匹配,您得到一个异常。

Fortunately, there is an overload for the method that accepts multiple format strings .幸运的是,接受多种格式字符串的方法有一个重载 Even with this overload, though, you probably need to take the time to review the csv data and make sure that every possible format in the data is accounted for.不过,即使有这种超载,您可能也需要花时间查看 csv 数据,并确保考虑到数据中的每种可能格式

And if data for this field could be entered by random humans may God have mercy on your soul.如果这个字段的数据可以由随机的人输入,愿上帝怜悯你的灵魂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图减去 DateTime “System.FormatException: String '12/9/2019 12:00:00 AM' 未被识别为有效的 DateTime。” - Trying to subtract DateTime "System.FormatException: String '12/9/2019 12:00:00 AM' was not recognized as a valid DateTime." 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# 如何解决此错误:System.FormatException:'字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.' “System.FormatException:‘该字符串未被识别为有效的 DateTime。有一个从索引 0 开始的未知单词。” 在 C# - "System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." in C# System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime System.FormatException:字符串未被识别为有效的布尔值 - System.FormatException: String was not recognized as a valid Boolean FormatException:该字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词 - FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM