C#SQL Server 2008 DATETIME2列

时间:2011-04-12 14:57:32

标签: c# sql-server-2008

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

1 个答案:

答案 0 :(得分:2)

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

.NET参考说:

  

价值的小数部分是   小部分毫秒。对于   例如,4.5相当于4   毫秒和5000个刻度,其中一个   毫秒= 10000滴答。

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