I am exporting a string formatted DateTime to a SQL Server record from my ASP.NET(vb.net) application. This works well on my development machine. I can use any formatting and change the culture in either the web.config or my <%@ Page %> declaration.
If I deploy the application to my production server these settings are disregarded. The date time will always be generated in this format "May 7 2014 7:31PM". No culture setting and no toString format shows any effect.
I am not so much fussed about the exact format but I need to get the seconds in the DateTime string. This just seems impossible on the production server.
Edit:
There isn't much code to show. The following line is working fine on the development machine:
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss.fff")
The point is that no format is ever applied on the production server.
Changing the culture doesn't have an effect neither (on the production server).
Edit:
In the page declaration I use:
<%@ Page Language="vb" Culture="es-MX" UICulture="es" ...
In the config I use:
<system.web>
<globalization culture="es-MX" uiCulture="es"/>
Rather than relying on configuration, consider changing your code to pass a specific culture.
For example:
yourDateTime.ToString("yyyy/MM/dd hh:mm:ss.fff", CultureInfo.InvariantCulture)
Or
yourDateTime.ToString("yyyy/MM/dd hh:mm:ss.fff", New CultureInfo("en-GB"))
UPDATE
Based on your comments below, I can offer the following advice:
Culture isn't relevant for this scenario. SQL Server doesn't store dates as strings, it stores them in types like datetime
or datetime2
or datetimeoffset
. These types have no culture-specific attributes.
You should never pass dates as strings when using SQL parameters. Pass them as DateTime
or DateTimeOffset
types.
You probably don't need to pass it at all, as you can just use GETDATE()
or GETUTCDATE()
in SQL.
I think what you mean is that when you look at the value in another tool like SQL Server Management Studio that you're seeing the output in a string with a particular format. That format has nothing to do with what is actually stored, only with how it is displayed . SQL has it's own settings for controlling that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.