问题
I'm working with MVC3 and Entity Framework. 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.
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)
Error says
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
I understood error is because of the difference between date time formats between System.Datetime
and 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.
Is there any workaround for this issue?
回答1:
My issue is resolved now. Issue was with the culture settings in IIS. I've added these line to my applications web.config and it is working fine now.
<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
回答2:
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.
//Here an example of formatting with invariant culture
CultureInfo yourCulture = CultureInfo.InvariantCulture;
yourValue.ToString("yourFormat",yourCulture));
回答3:
Verify that the date time formatting. Also, DateTime
doesn't have the same range as SqlDateTime
. You'll run into trouble if you receive an empty value.
来源:https://stackoverflow.com/questions/12017967/entity-framework-sql-server-2005-iis-server-datetime-issue