在 C# 中将 DateTime 转换为 SmallDateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16945111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:11:55  来源:igfitidea点击:

Converting DateTime to SmallDateTime in c#

c#asp.netentity-framework

提问by kapozade

How can ? convert datetime to smalldatetime in c# ? I'm taking the date and ? need to convert it to be accordance with database. It is forbidden to change the datatype of column in sql.

怎么能 ?在 C# 中将 datetime 转换为 smalldatetime ?我要约会和?需要将其转换为与数据库一致。禁止在sql中更改列的数据类型。

采纳答案by Myk Willis

You can use the .NET DateTimetype for your entity framework model, but tell EF that it uses a non-default column type in the database. You do this by overriding the OnModelCreatingmethod of your DbContext, and using the HasColumnTypemethod:

您可以为实体框架模型使用 .NET DateTime类型,但告诉 EF 它在数据库中使用非默认列类型。您可以通过覆盖DbContext 的 OnModelCreating方法并使用HasColumnType方法来完成此操作

public class Foo    
{
    public int Id { get; set; }
    public DateTime IAmSoSmall { get; set; }    // wants to be smalldatetime in SQL
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var foo = modelBuilder.Entity<Foo>();
        foo.Property(f => f.IAmSoSmall).HasColumnType("smalldatetime");

        base.OnModelCreating(modelBuilder);
    }
}

Of course, you'll have to do the appropriate range-checking on your DateTime property to be sure that the stored values fall between those supported by SQL's smalldatetime. I guess you could do that with a property attribute like:

当然,您必须对 DateTime 属性进行适当的范围检查,以确保存储的值介于 SQL 的 smalldatetime 支持的值之间。我想你可以用一个属性属性来做到这一点:

    [Range(typeof(DateTime), "1/1/1900", "6/6/2079")]
    public DateTime IAmSoSmall { get; set; }    // wants to be smalldatetime in SQL

...based on a valid range from January 1, 1900, through June 6, 2079, as documented on MSDN.

...基于从 1900 年 1 月 1 日到 2079 年 6 月 6 日的有效范围,如 MSDN 上所述。

回答by user2335149

Maybe you can do something like YourDateTime.Date

也许你可以做一些类似 YourDateTime.Date

Normally, when you do that way, it will set time to 00:00:00.

通常,当您这样做时,它会将时间设置为 00:00:00。

回答by Nicholas Carey

Sql Server datetimeand smalldatetimeare both automaticallymapped to and from the CLR's System.DateTime. A smalldatetimehas a precision of 1 minute; a datetimehas a precision of approximately 1/300 of a second (Don't ask why. It just is). Since the CLR's System.DateTime1has a precision of 100-nanoseconds, the runtime takes care of rounding.

Sql Serverdatetime并且smalldatetime自动映射到 CLR 的System.DateTime. Asmalldatetime的精度为 1 分钟;adatetime具有大约 1/300 秒的精度(不要问为什么。它就是这样)。由于 CLR 的System.DateTime1精度为 100 纳秒,因此运行时负责舍入。

  • smalldatetimeis internally a 32-bit integer, containing a count of minutes since the smalldatetimeepoch (1900-01-01 00:00).

    In conversion, seconds and fractional seconds are rounded using SQL Server's arcane date/time rounding rules, so the date 2013-01-31 23:59:59 gets rounded to the next date 2013-02-01 00:00:00'.

  • datetimeis a pair of 32-bit integers internally. The high-order word is a count of days since the epoch; the low-order word is a count of milliseconds since start-of-day (00:00:00). The epoch of a datetimeis 1900-01-01 00:00:00.000.

    And again, values are rounded in the conversion in the same arcane way, with franctional seconds getting placed into one of the appropriate millisecond buckets for SQL Server, a multiple of 3ms — there is no SQL Server `datetime value like 2013-05-01 13:57:23.004. That will get "rounded" to either 23.003ms or 23.006ms.

  • smalldatetime内部是一个 32 位整数,包含自smalldatetime纪元 (1900-01-01 00:00)以来的分钟数。

    在转换中,秒和小数秒使用 SQL Server 的神秘日期/时间舍入规则舍入,因此日期 2013-01-31 23:59:59 被舍入到下一个日期2013-02-01 00:00:00'。

  • datetime内部是一对 32 位整数。高位词是自纪元以来的天数;低位字是自一天开始 (00:00:00) 以来的毫秒数。a 的纪元datetime是 1900-01-01 00:00:00.000。

    再次,值在转换中以同样神秘的方式四舍五入,小数秒被放置到 SQL Server 的适当毫秒桶之一中,3ms 的倍数 - 没有像 2013-05-01 这样的 SQL Server `datetime 值13:57:23.004。这将“四舍五入”到 23.003 毫秒或 23.006 毫秒。

If you want more control over things, you'll need to adjust your datetime values in C# before sending them to the database.

如果您想对事物进行更多控制,则需要在将它们发送到数据库之前在 C# 中调整日期时间值。