C# SQL Server 2008 DATETIME2 列

标签 c# sql-server-2008

如何通过 LINQ 在 C# 中使用 DATETIME2 列并获得微秒分辨率?现在,即使服务器数据类型设置为 DATETIME2,我的 LINQ 对象也有一个 DateTime 字段,它似乎不处理微秒。

最佳答案

即使 DATETIME2 处理微秒,.NET 4 也不会。如果需要专门处理微秒,则可以在 SQL Server 中使用过程,而无需通过应用程序读取值。

.NET 引用说:

The fractional part of value is the fractional part of a millisecond. For example, 4.5 is equivalent to 4 milliseconds and 5000 ticks, where one millisecond = 10000 ticks.

string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff"; 
DateTime date1 = new DateTime(2010, 9, 8, 16, 0, 0);
Console.WriteLine("Original date: {0} ({1:N0} ticks)\n",
                  date1.ToString(dateFormat), date1.Ticks);

DateTime date2 = date1.AddMilliseconds(1);
Console.WriteLine("Second date:   {0} ({1:N0} ticks)",
                  date2.ToString(dateFormat), date2.Ticks);
Console.WriteLine("Difference between dates: {0} ({1:N0} ticks)\n",
                  date2 - date1, date2.Ticks - date1.Ticks);                        

DateTime date3 = date1.AddMilliseconds(1.5);
Console.WriteLine("Third date:    {0} ({1:N0} ticks)",
                  date3.ToString(dateFormat), date3.Ticks);
Console.WriteLine("Difference between dates: {0} ({1:N0} ticks)",
                  date3 - date1, date3.Ticks - date1.Ticks);                        
// The example displays the following output:
//    Original date: 09/08/2010 04:00:00.0000000 (634,195,584,000,000,000 ticks)
//    
//    Second date:   09/08/2010 04:00:00.0010000 (634,195,584,000,010,000 ticks)
//    Difference between dates: 00:00:00.0010000 (10,000 ticks)
//    
//    Third date:    09/08/2010 04:00:00.0020000 (634,195,584,000,020,000 ticks)
//    Difference between dates: 00:00:00.0020000 (20,000 ticks)  

引用:DateTime.AddMilliseconds Method

关于C# SQL Server 2008 DATETIME2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5637301/

相关文章:

c# - 无法解析目标框架 '.NETFramework,Version=v4.6.1' 的 mscorlib

c# - 使用 MS bot 框架获取用户团队列表

sql-server-2008 - 选择值大于 0 的列

sql - 区分ß和ss的SQL查询

c# - 有没有 asp.net fiddle ?

c# - 在 C# 中,什么时候对象符合垃圾回收条件?

c# - 为什么我要使用托管(C# 和 .NET)或 native 代码进行 Windows API 开发?

sql-server - 是否可以明确识别 DML 命令是否是从存储过程发出的?

sql - TSQL Shred XML - 使用 namespace

xml - XQuery [节点 ()] : The name "s" does not denote a namespace