Hello I have little problem with DateTime
formats in SQL server
.
I have an app which connects with SQL Server 2012
.
I need to use EU Dateformat in App. SQL server holds DateTime in EU Format
proof:
But when I allow users to enter date in EU format EntityFramework
throws Model not valid
I suppose that C# default DateTimeformat is US
how to modify this code to let user enter EU format date and pass it to DB without any problems:
My model:
namespace magazyn.Models
{
using System;
using System.Collections.Generic;
public partial class DeviceInstance
{
public int Id { get; set; }
public int DeviceId { get; set; }
public string SerialNo { get; set; }
public System.DateTime CreationDate { get; set; }
public virtual Device Device { get; set; }
public virtual DeviceUsage DeviceUsage { get; set; }
}
}
SQL server holds DateTime in EU Format
No, it doesn't. (At least, not if it's in an appropriate column type in the database.) It holds it in a more compact binary format - at least so I assume. That's an implementation detail though. Fundamentally, the point is that it's a date/time value. You should always talk to the database in terms of a DateTime
value (or DateTimeOffset
or whatever), not in terms of a string. That way, you won't get involved in conversion. What you see in your SQL Explorer (or whatever you're using to query it) is just a string representation of what's in the database. That isn't the same as the value that's in the database.
In the same way, you could ask the database to display a number as decimal or hex - that doesn't change the fundamental numeric value.
You need to perform an appropriate conversion of the user-entered data in your application, and again to display any data from the database to the user, but there should be no need to use a string representation beyond that "user boundary".
You should try to make the vast majority of your code work with a non-string-based representation. Convert input data to the "natural" representation as early as you possibly can, and convert output data to a culture-specific string representation as late as you possibly can.
See more on this question at Stackoverflow