简体   繁体   English

Windows 服务给出的字符串未被识别为有效的 DateTime 异常,但相同的代码在控制台应用程序中正常运行

[英]Windows Service gives String was not recognized as a valid DateTime exception but the same code running properly in Console Application

Windows Service gives String was not recognized as a valid DateTime exception but the same code running properly in Console Application Windows 服务给出的字符串未被识别为有效的 DateTime 异常,但相同的代码在控制台应用程序中正常运行

Object max = cmd.ExecuteScalar();   //max will have 6/30/2012 12:00:00 AM 
DateTime currentDt = DateTime.Now;
currentDt = DateTime.ParseExact(max.ToString(), "M/d/yyyy h:mm:ss tt", CultureInfo.CurrentCulture.DateTimeFormat);                         //This Line Gives Error in WindowsService Only
StreamWriter sw = new StreamWriter("E:\\ram\\SampleService.txt", true);
sw.WriteLine(currentDt.ToString());
sw.Close();

I even Changed System DateTime Format Settings to Engish - Us Settings.ShortDatetime is M/d/yyyy and Longtime is h:mm:ss tt.我什至将系统日期时间格式设置更改为英文 - Us Settings.ShortDatetime 是 M/d/yyyy,Longtime 是 h:mm:ss tt。

Can someone help me resolve this issue?有人可以帮我解决这个问题吗?

My guess is that the system locale isn't the same as your user locale.我的猜测是系统区域设置与您的用户区域设置不同。 If your system locale uses something other than "/" as the date separator, it will fail to match the "/" in your format string.如果您的系统区域设置使用除“/”以外的其他内容作为日期分隔符,它将无法匹配格式字符串中的“/”。

I suggest you change to use CultureInfo.InvariantCulture , at which point it should work - if the value of max.ToString() is actually "6/30/2012 12:00:00 AM".我建议您更改为使用CultureInfo.InvariantCulture ,此时它应该可以工作 -如果max.ToString()值实际上是“6/30/2012 12:00:00 AM”。 Have you validated that in the case it's failing, that's the value you're getting?你有没有验证过,在它失败的情况下,这就是你得到的价值?

If your value is coming from a database though, why is it stored as a string to start with?如果您的值来自数据库,为什么它以字符串开头? Are you sure it even is a string?你确定它甚至一个字符串? If it's actually a DateTime , then when you call ToString() you'll be using the current culture's default format to convert it - which could easily fail on the way back.如果它实际上是一个DateTime ,那么当您调用ToString()您将使用当前文化的默认格式来转换它 - 这很容易在返回的路上失败。 Even if it is a string at the moment, does it really have to be?哪怕此刻一串串串,真的非得如此吗? The fewer string conversions you can introduce, the better.您可以引入的字符串转换越少越好。

(As an aside, it's simpler to use File.WriteAllText or File.AppendAllText than using a StreamWriter like this. If you do need to use a StreamWriter , remember to use a using statement to dispose of the resource properly.) File.WriteAllText File.AppendAllText ,使用File.WriteAllTextFile.AppendAllText比使用这样的StreamWriter更简单。如果您确实需要使用StreamWriter ,请记住使用using语句正确处理资源。)

在没有 CultureInfo 的情况下尝试:

currentDt = DateTime.ParseExact(max.ToString(), "M/d/yyyy h:mm:ss tt", null);

Does your SQL request return DateTime or String value?您的 SQL 请求是否返回DateTimeString值?

Also I don't see why do you use ParseExact in this case.另外我不明白你为什么在这种情况下使用ParseExact In your case I would suggest to use Parse method with InvariantCulture parameter:在您的情况下,我建议使用带有InvariantCulture参数的 Parse 方法:

currentDt = DateTime.Parse(max.ToString(), CultureInfo.InvariantCulture);

If your max value is DateTime then you don't need to do parsing at all.如果您的max是 DateTime,那么您根本不需要进行解析。 You just need to check if it is DBNull.Value:你只需要检查它是否是 DBNull.Value:

if (max != DBNullValue)
{
    currentDt = (DateTime)max;
}

I got same problem when i had been building windows service.In that, i tryed to convert string date to datetime format.我在构建 Windows 服务时遇到了同样的问题。在那,我尝试将字符串日期转换为日期时间格式。 For that i used format string date_time = Day + "/" + Mn + "/" + Yr + " " + Hr + ":" + Min;为此,我使用格式字符串 date_time = Day + "/" + Mn + "/" + Yr + " " + Hr + ":" + Min; and tryed convert it to datetime().并尝试将其转换为 datetime()。 This format was running properly in windows form application but not in Windows service project.此格式在 Windows 窗体应用程序中正常运行,但在 Windows 服务项目中未正常运行。

Solution :解决方案 :

I changed above string format with this format string date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min;我用这个格式字符串 date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min更改了上面的字符串格式 , And then passed it to datetime(). , 然后将其传递给 datetime()。 Like follow :喜欢跟随:

string date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min; string date_time = Mn + "/" + Day + "/" + Yr + " " + Hr + ":" + Min; DateTime dt_1 = Convert.ToDateTime(date_time);日期时间 dt_1 = Convert.ToDateTime(date_time);

By default the en-US culture is used by .NET according to which the Date is in Month/Day/Year format默认情况下,.NET 使用 en-US 文化,根据该文化,日期采用月/日/年格式

This is 100% working.这是 100% 工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM