Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#和SQL Server中的DateTimeOffset解析_C#_.net_Sql Server 2008_Datetimeoffset_Time Precision - Fatal编程技术网

c#和SQL Server中的DateTimeOffset解析

c#和SQL Server中的DateTimeOffset解析,c#,.net,sql-server-2008,datetimeoffset,time-precision,C#,.net,Sql Server 2008,Datetimeoffset,Time Precision,文档指出,在.NET和SQL server中,分辨率均为100ns DateTimeOffset值的时间分量是以100纳秒为单位测量的,称为ticks-C# 精度-100纳秒-SQL Server 但是,SQL似乎删除了最后一个数字(例如,我试图保存2013-08-15 09:19:07.2459675-04:00,SQL保存2013-08-15 09:19:07.2459670-04:00-注意最后一个数字的更改。) 这发生在同一台机器上,因此不依赖于硬件 并不是说我真的需要这个决议,但它使比

文档指出,在.NET和SQL server中,分辨率均为100ns

DateTimeOffset值的时间分量是以100纳秒为单位测量的,称为ticks-C# 精度-100纳秒-SQL Server

但是,SQL似乎删除了最后一个数字(例如,我试图保存2013-08-15 09:19:07.2459675-04:00,SQL保存2013-08-15 09:19:07.2459670-04:00-注意最后一个数字的更改。)

这发生在同一台机器上,因此不依赖于硬件


并不是说我真的需要这个决议,但它使比较日期变得更加困难。。我只是好奇。

我会说问题是你的。。。要显示的小代码:

namespace Test
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;

    /// <summary>
    /// 
    /// </summary>
    public class Program
    {
        /// <summary>
        /// 
        /// </summary>
        public static void Main()
        {
            // Change the connection string to specify your server. 
            // Probably you won't need an initial catalog because this
            // program uses a temp table
            string connStr = "Integrated Security=True";

            // The temp table is called #Temp . It will cease to exist at the end 
            // of the program automatically
            // Two columns, DateTimeOffset and ShortDateTimeOffset
            string query = @"CREATE TABLE #Temp (DateTimeOffset datetimeoffset(7) NOT NULL, ShortDateTimeOffset datetimeoffset(6) NOT NULL);INSERT INTO #Temp VALUES (@DT1, @DT2);SELECT * FROM #Temp";

            using (var connection = new SqlConnection(connStr))
            using (var command = new SqlCommand(query, connection))
            {
                const string dtString = "2013-08-15 09:19:07.2459675 -04:00";
                const string dtFormat = "yyyy-MM-dd HH:mm:ss.fffffff zzz";

                DateTimeOffset dt = DateTimeOffset.Parse(dtString, CultureInfo.InvariantCulture);

                string dtString2 = dt.ToString(dtFormat, CultureInfo.InvariantCulture);

                Console.WriteLine("Sending          : {0}", dtString2);

                // Just to be sure!
                if (dtString != dtString2)
                {
                    throw new Exception("Problem in conversion");
                }

                command.Parameters.Add("@DT1", SqlDbType.DateTimeOffset).Value = dt;
                command.Parameters.Add("@DT2", SqlDbType.DateTimeOffset).Value = dt;

                try
                {
                    connection.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            DateTimeOffset dtRec1 = (DateTimeOffset)reader[0];
                            DateTimeOffset dtRec2 = (DateTimeOffset)reader[1];

                            string dtRecString1 = dtRec1.ToString(dtFormat, CultureInfo.InvariantCulture);
                            string dtRecString2 = dtRec2.ToString(dtFormat, CultureInfo.InvariantCulture);

                            Console.WriteLine("Receiving (long) : {0}", dtRecString1);
                            Console.WriteLine("Receiving (short): {0}", dtRecString2);

                            if (dtRec1 != dt)
                            {
                                throw new Exception("Difference between DateTimeOffset(.NET) and DateTimeOffset(sql)");
                            }

                            if (Math.Abs(dtRec2.Ticks - dt.Ticks) > 10)
                            {
                                throw new Exception("Too much difference between DateTimeOffset(.NET) and DateTimeOffset(6)(sql)");
                            }

                            if (reader.Read())
                            {
                                throw new Exception("Too many rows");
                            }
                        }
                        else
                        {
                            throw new Exception("No rows");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

“short”是一个
DateTimeOffset(6)

他们都说DateTimeOffset,不是吗?是DateTimeOffset(7)。是的,它确实是我的。我们正在使用带有一些自定义扩展名的ORM,DateTimeOffset的格式使用了一个太短的分数(“yyyy-MM-dd-HH:MM:ss.ffffff-zzz”)。因此,即使是datetimeoffset(7),它也在删除最后一个数字。
Sending          : 2013-08-15 09:19:07.2459675 -04:00
Receiving (long) : 2013-08-15 09:19:07.2459675 -04:00
Receiving (short): 2013-08-15 09:19:07.2459680 -04:00