简体   繁体   English

SqlParameter Datetime SqlValue与值的差异

[英]SqlParameter Datetime SqlValue difference with Value

I have a datetime value to pass into a SqlParameter . 我有一个datetime值传递给SqlParameter

DateTime object value DateTime对象的值

However when pass to myCmd.Parameters.Add("@TrxDate", adt_TrxDate); 但是,当传递给myCmd.Parameters.Add("@TrxDate", adt_TrxDate);

SqlParameter SqlValue & Value property is difference value SqlParameter SqlValue&Value属性为差值

Found that it add a day when executing query. 发现它在执行查询时增加了一天。 I found this issue in profiler and finally find out SqlParameter SqlValue & Value property is different value. 我在探查器中发现了这个问题,最后发现SqlParameter SqlValue和Value属性是不同的值。 Why it is happen and any idea? 为什么会发生,有什么想法?

Root cause is, .NET DateTime has higher precision then SQL Server's DateTime. 根本原因是,.NET DateTime比SQL Server的DateTime精度更高。 So it is rounded off. 因此,四舍五入。 SQL Server 2008 on wards, DateTime2 supports higher precision. 在SQL Server 2008病房中,DateTime2支持更高的精度。

Since data type is DateTime in SQL, SQL Parameter is rounding .net DateTime to nearest Sql DateTime. 由于SQL中的数据类型为DateTime,因此SQL参数将.net DateTime舍入为最接近的Sql DateTime。 Since it is rounding algorithm, it may add .003,.007 or remove, you can look at this SO question for more details. 由于它是舍入算法,因此可能会添加.003,.007或将其删除,您可以查看此SO问题以获取更多详细信息。 Undesired rounding of DateTime in SQL Server , so sometimes, if added micro seconds adds up, it may actually change to next day if it was on boundary between two days. 在SQL Server中对DateTime进行不希望的舍入 ,因此有时,如果增加了微秒,则实际上它可能会更改为第二天(如果介于两天之间)。

From MSDN, 从MSDN,

https://msdn.microsoft.com/en-us/library/system.data.sqldbtype(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.data.sqldbtype(v=vs.110).aspx

DateTime. 约会时间。 Date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds. 日期和时间数据,取值范围为1753年1月1日至9999年12月31日,精度为3.33毫秒。

DateTime2 Date and time data. DateTime2日期和时间数据。 Date value range is from January 1,1 AD through December 31, 9999 AD. 日期值范围是从公元1月1,1日到9999年12月31日。 Time value range is 00:00:00 through 23:59:59.9999999 with an accuracy of 100 nanoseconds. 时间值范围是00:00:00到23:59:59.9999999,精度为100纳秒。

So I guess if you change Parameter Type to DateTime2 it would preserve actual time. 因此,我想如果将Parameter Type更改为DateTime2,它将保留实际时间。 However, unless your SQL Server has column's type as DateTime2, it this will have no effect. 但是,除非您的SQL Server的列类型为DateTime2,否则它将无效。

You have to use like the following by specifying the SqlDbType for the parameter : 您必须通过为参数指定SqlDbType来使用以下内容:

   myCmd.Parameters.Add("@TrxDate",SqlDbType.Date).Value =adt_TrxDate;

use SqlDbType.DateTime if it is DateTime 如果它是DateTime使用SqlDbType.DateTime

or you can use AddWithValue 或者您可以使用AddWithValue

  myCmd.Parameters.AddWithValue("@TrxDate",adt_TrxDate);

You can refer this thread for the difference between these two 您可以参考此线程以了解两者之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM