简体   繁体   English

实体框架-SQL Server 2005-IIS服务器日期时间问题

[英]Entity Framework - SQL Server 2005 - IIS Server datetime issue

I'm working with MVC3 and Entity Framework. 我正在使用MVC3和实体框架。 In my application I need to call a stored procedure in SQL Server 2005 via EF to search for some data according to datetime parameters passed. 在我的应用程序中,我需要通过EF调用SQL Server 2005中的存储过程,以根据传递的datetime参数搜索某些数据。

Everything seems to be working fine in local environment. 在本地环境中,一切似乎都正常。 But after hosting it into IIS I am getting an exception while trying to search from date 13-08-2012 (13 is taking as month in SQL I guess) 但是将其托管到IIS后,尝试从13-08-2012 13月8日13-08-2012进行搜索时遇到了异常(我想SQL 13相当于一个月)

Error says 错误说

SqlDateTime overflow. SqlDateTime溢出。 Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM 必须介于1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间

I understood error is because of the difference between date time formats between System.Datetime and SqlDatetime . 我了解错误是由于System.DatetimeSqlDatetime之间的日期时间格式之间的SqlDatetime

But I didn't understand why it is working without any issues in my local environment which uses same SQL Server but getting this error after hosting in IIS server. 但是我不明白为什么它在使用相同SQL Server的本地环境中运行时没有任何问题,但是在IIS服务器中托管后却出现此错误。

Is there any workaround for this issue? 有没有解决此问题的方法?

My issue is resolved now. 我的问题现在已经解决。 Issue was with the culture settings in IIS. 问题出在IIS中的区域性设置。 I've added these line to my applications web.config and it is working fine now. 我已经将这些行添加到我的应用程序web.config中,并且现在可以正常工作。

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>

For more information check out this Issue with culture settings in IIS 有关更多信息,请查看IIS中的区域性设置问题

IT depends on the culture of the server, you can format your culture with invariant format, on your date 13-08-2012, it consider 13 as month. IT取决于服务器的区域性,您可以使用不变格式来格式化区域性,在日期13-08-2012上,它将13视为月份。

//Here an example of formatting with invariant culture
CultureInfo yourCulture = CultureInfo.InvariantCulture;
yourValue.ToString("yourFormat",yourCulture));

Verify that the date time formatting. 验证日期时间格式。 Also, DateTime doesn't have the same range as SqlDateTime . 另外, DateTimeSqlDateTime范围不同。 You'll run into trouble if you receive an empty value. 如果您收到一个空值,将会遇到麻烦。

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

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