c# - 如何将字节数组(SQL 服务器时间戳)转换为日期时间(C#)?

标签 c# sql-server entity-framework

如何将字节数组转换为 DateTime?我必须这样做,因为我使用属性 (名为 CreationDataStamp) 和 Entity Framework 的属性 Timestamp。第二个属性(名为CreationDate)我用于将字节数组转换为DateTime

[System.ComponentModel.DataAnnotations.Timestamp]
public byte[] CreationDateStamp { get; set; }

[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public DateTime CreationDate
{
    get
    {
        return Convert.ToDateTime(CreationDateStamp);
    }
}

问题是我在第 9 行有这个异常:

Unable to cast object of type System.Byte[] to type System.IConvertible.


更新:

我也试过这个问题:How to convert a byte[] into datetime in C#?

long longVar = BitConverter.ToInt64(CreationDateStamp, 0);
DateTime dateTimeVar = new DateTime(1980,1,1).AddMilliseconds(longVar);

但我有这个异常(exception):

Value to add was out of range.
Parameter name: value

你能帮帮我吗?

最佳答案

Sql server Timestamp 不是 DateTime。它是元组(行)的版本控制列。

简而言之,你不能做你想做的事。

关于c# - 如何将字节数组(SQL 服务器时间戳)转换为日期时间(C#)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465241/

相关文章:

c# - Entity Framework Core 中的内存管理

c# - 从 KeyMembers 中获取身份字段

c# - C#中的奇怪枚举名称解析

c# - 根据数组成员的其他属性在 Unity 编辑器中显示数组成员的某些属性

c# - 保护使用使用 twitter 登录的应用程序的 API

SQL 创建数据库如果不存在,意外行为

sql-server - Grails应用程序连接到SQL Server

c# - 将 Powerpoint 演示嵌入到 C# 应用程序中

c# - 如何使用 FluentMigrator 将值更新为 null

entity-framework - 将 EF 迁移合并到新的 InitialCreate 中