Sql server 如何将[Log Content 0]中fn_dblog()详细信息中的8字节datetime转换为C#datetime对象?

Sql server 如何将[Log Content 0]中fn_dblog()详细信息中的8字节datetime转换为C#datetime对象?,sql-server,datetime,sql-server-2008-r2,transaction-log,transactionloganalysis,Sql Server,Datetime,Sql Server 2008 R2,Transaction Log,Transactionloganalysis,我删除了最近插入的一行数据。 我尝试使用fn_dblog()。 使用说明(可在此处找到:) 在[Log Content 0]列fn_dblog()返回的内容中,我正在成功地从日志文件中检索插入的(以及后来删除的)数据。在这个为固定宽度列数据保留的二进制数据部分中,我发现SQL DateTime列值需要8个字节。我在.NET程序中处理二进制数据,对Int或BigInt值使用BitConverter.ToInt64或BitConverter.ToInt32 我已设法检索除datetime列之外所需的

我删除了最近插入的一行数据。 我尝试使用fn_dblog()。 使用说明(可在此处找到:)

在[Log Content 0]列fn_dblog()返回的内容中,我正在成功地从日志文件中检索插入的(以及后来删除的)数据。在这个为固定宽度列数据保留的二进制数据部分中,我发现SQL DateTime列值需要8个字节。我在.NET程序中处理二进制数据,对Int或BigInt值使用BitConverter.ToInt64或BitConverter.ToInt32

我已设法检索除datetime列之外所需的所有插入列值

我不清楚如何将SQL DateTime列的8个字节解释为C#DateTime对象。如果有帮助,下面是从特定日期时间的事务日志数据中检索到的日期时间8字节的hex和Int64版本示例

二进制日期时间(大约2020年7月31日):0xF030660009AC0000(Endian反转:0x0000AC09006630F0)

作为一个Int64:18915466138084


有什么建议吗?这是一个日期的内部SQL Server表示,我不确定在哪里可以找到它上面的文档…

我终于找到了答案:存储为VARBINARY的SQL DateTime(类似于我从事务日志中读取的字节)包含两个整数。第一个是日期部分-自1900年1月1日起的天数。对于较早的日期,它将为负值

第二个整数是午夜后的毫秒数除以3.33333

因为字节是以长字节的形式存储的,所以缓冲区中8个字节中的前4个字节是分钟,第二个是日期

下面是我用来获取日期的代码片段。我一次遍历一个固定长度字段,跟踪字节数组中的当前偏移量。。。 变量ba是[Log Content 0]列中字节的字节数组

        int TimeInt;
        int DateInt;
        DateTime tmpDt;

        //initialize the starting point for datetime - 1/1/1900
        tmpDt = new DateTime(1900, 1, 1);
        // get the time portion of the SQL DateTime
        TimeInt = BitConverter.ToInt32(ba, currOffset);
        currOffset += 4;
        // get the date portion of the SQL DateTime
        DateInt = BitConverter.ToInt32(ba, currOffset);
        currOffset += 4;
        // Add the number of days since 1/1/1900
        tmpDt = tmpDt.AddDays(DateInt);
        // Add the number of milliseconds since midnight
        tmpDt = tmpDt.AddMilliseconds(TimeInt * 3.3333333);