Aks*_*y J 7 c# sql-server datetime
当我插入这些值
09/30/2013 05:04:56.599
09/30/2013 05:04:56.599
09/30/2013 05:04:56.599
09/30/2013 05:04:57.082
Run Code Online (Sandbox Code Playgroud)
在SqlServer数据库中,毫秒值以奇怪的方式变化
2013-09-30 05:04:56.600
2013-09-30 05:04:56.600
2013-09-30 05:04:56.600
2013-09-30 05:04:57.083
Run Code Online (Sandbox Code Playgroud)
怎么了 ?
编辑:相关代码:
com = new SqlCommand();
com.Connection = con;
com.CommandText = @"INSERT INTO [AuthSourceTimings]
([FileName]
,[JobID]
,[JobCreationTime]
,[JobSendTime]
,[JobAckTime]
,[JobDoneTime])
VALUES
(@FileName
,@JobID
,@JobCreationTime
,@JobSendTime
,@JobAckTime
,@JobDoneTime)
";
com.Parameters.AddWithValue("@FileName", fileName);
com.Parameters.AddWithValue("@JobID", t.JobID);
com.Parameters.AddWithValue("@JobCreationTime", t.JobCreationTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobCreationTime);
com.Parameters.AddWithValue("@JobSendTime", t.JobSendTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobSendTime);
com.Parameters.AddWithValue("@JobAckTime", t.JobAcknowledgementTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobAcknowledgementTime);
com.Parameters.AddWithValue("@JobDoneTime", t.JobCompletionTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobCompletionTime);
com.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
毫秒数仅以日期时间格式的约1/300秒的精度存储,因此不准确的地方来自.
您可以查看以下答案:为什么SQL Server会丢失一毫秒?
要获得更高的精度(100纳秒),您可以使用DATETIME2SQL Server 2008中引入的精度.您可以在此处获取更多信息:
您可能正在使用SQL Server类型DATETIME,文档指出:
Rounded to increments of .000, .003, or .007 seconds.
Run Code Online (Sandbox Code Playgroud)
DATETIME2如果要与.NET完全兼容,则应使用该类型DateTime.这两者都具有100纳秒的精度.
使用的原因并不多DATETIME.A DATETIME2(7)占用8个字节,与a相同DATETIME,但具有更高的精度和范围.A DATETIME2(3)占用6个字节并且仍然具有更好的准确性(3个小数,没有上面详述的奇怪的舍入行为).
| 归档时间: |
|
| 查看次数: |
305 次 |
| 最近记录: |