日期时间插入,然后选择:序列不包含任何元素 [英] DateTime insertion and then selection: sequence contains no elements
问题描述
在SQL Server中我的表结构是:
My table structure in sql server is :
TableId int (Pk) identity
Data string
DateNTime DateTime
我的方法是:
My method is::
public int insertData(string data){
Date= DateTime.Now;
Table table= new Table();
table.Data= data;
table.DateNTime=Date;
this.DataContext.Set<Table>().Add(table);
this.DataContext.SaveChanges();
return this.DataContext.Tables.Single(b => b.DateNTime == Date).TableId
}
数据插入,而不会产生任何问题,但在9个10倍,而返回TABLEID我得到的异常序列不包含任何元素
The data insertion happens without any problems but in 9 out of 10 times while returning TableId I get the exception "sequence contains no elements"
难道说该表行被保存之前选择命令被触发,我得到这个错误,如果是的话我该怎么办?
Could it be that before the table row is saved the select command is fired and I get this error, if so what do I do?
谢谢
ARNAB
Thanks Arnab
推荐答案
的SaveChanges()
是一个同步的堵的操作,它不会在交易前返回保存日期承诺。所以,当你调用查询的日期肯定是保存在数据库中。
SaveChanges()
is a synchronous "blocking" operation, it doesn't return before the transaction that saves the date is committed. So, when you call the query the date is definitely saved in the database.
我觉得,这是一个precision /四舍五入问题。如果你看一下的precision一个的DateTime
在.NET中你会看到(比如在的TimeOfDay
属性):
I think, it is a precision/rounding problem. If you look at the precision of a DateTime
in .NET you'll see (for example in the TimeOfDay
property):
.NET的的TimeOfDay 的DateTime
键入:10:32:51.0312500
TimeOfDay of .NET DateTime
type: 10:32:51.0312500
因此,precision是10E-7秒。 A 日期时间
在SQL Server只有10E,在3秒precision和.NET 的DateTime
保存这样的数据库中的
So, the precision is 10E-7 seconds. A datetime
in SQL Server has only 10E-3 seconds precision and the .NET DateTime
is saved like this in the database:
的SQL Server的列值日期时间
键入:10:32:51.030
Column value of SQL Server datetime
type: 10:32:51.030
因此,它被四舍五入至三位数。当您运行查询.NET 的DateTime
与高precision(传输类型 DATETIME2(7)
在SQL Server)...
So, it is rounded to three digits. When you run the query the .NET DateTime
is transmitted with high precision (as type datetime2(7)
in SQL Server) ...
WHERE [Extent1].[MyDateTimeColumn] = @p__linq__0',
N'@p__linq__0 datetime2(7)', @p__linq__0='2012-05-18 10:32:51.0312500'
...和平等的比较失败,因为
... and the equality comparison fails because
10 2012-05-18:32:51.0312500
= 10 2012-05-18:32:51.030
如果你想要更高的precision使用 DATETIME2(7)
在SQL服务器类型的.NET匹配的DateTime
类型。或避免平等这样的查询,而是查询的在你的的DateTime
值+/- 1秒或什么的,像这样的间隔:
If you want a higher precision use a datetime2(7)
as type in SQL Server that matches the .NET DateTime
type. Or avoid such queries for equality and instead query for an interval of +/- 1 second or something around your DateTime
value, like so:
var date1 = Date.AddSeconds(-1);
var date2 = Date.AddSeconds( 1);
return this.DataContext.Tables
.Single(b => b.DateNTime >= date1 && b.DateNTime <= date2)
.TableId;
(当然不是一个很好的解决方案,如果您省下的每2秒更快,单
可能会失败,序列包含多个元素除外。)
(Not a good solution of course if you save faster than every 2nd second, Single
might fail with "Sequence contains more than one element" exception.)
这篇关于日期时间插入,然后选择:序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!