ADO.NET:超时查询数据表
我有一个内存中的 DataTable,其中包含两个 DateTime
类型的字段,分别是 LastAction 和 LastHeartbeat,表示实体上次发送活动日志消息的时间和实体上次发送心跳的时间(“I”我还活着”)日志消息。
我正在构建一种方法来使用各种条件查询数据表,包括系统中实体的“最大不活动时间”。此最大超时是为了同时考虑 LastAction 和 LastHeartbeat,因此查询将类似于 (LastAction > Now-timeout || LastHeartbeat > Now-timeout)
,目前还可以。超时值以整数秒表示。
目前,我分两行执行此操作
DateTime lastActivity = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(maxinactivity));
filters.Add(string.Format("({0} >= {2} OR {1} >= {2}}", _colLastAction.ColumnName, _colLastHeartbeat.ColumnName, lastActivity.ToString(CultureInfo.InvariantCulture)));
,最终结果将类似于 (LastAction >= #...# OR LastHeartbeat >= #...#)
,具体取决于查询的当前时间 我的问题
是:我可以在查询中进行日期减法吗?是否存在运算符,以便我可以直接在查询中输入超时?在 SQL 中,至少在 MySQL 中,我能够使用 NOW() 函数进行日期减法。 ADO.NET 中也可以吗?请记住,这是离线 ADO.NET 的内存表,因为数据并不意味着以任何方式持久保存。
谢谢
I have an in-memory DataTable that contains two fields of type DateTime
, respectively LastAction and LastHeartbeat meaning the last time an entity sent an activity log message and the last time the entity sent a heartbeat ("I'm alive") log message.
I'm building a method to query the data table with various criteria, including the "maximum inactivity time" of an entity in the system. This maximum timeout is meant in order to consider both LastAction and LastHeartbeat, so the query will resemble something like (LastAction > Now-timeout || LastHeartbeat > Now-timeout)
and that's OK for now. The timeout value is expressed in integer seconds.
Currently I do this in two lines
DateTime lastActivity = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(maxinactivity));
filters.Add(string.Format("({0} >= {2} OR {1} >= {2}}", _colLastAction.ColumnName, _colLastHeartbeat.ColumnName, lastActivity.ToString(CultureInfo.InvariantCulture)));
where the final result will look like (LastAction >= #...# OR LastHeartbeat >= #...#)
depending on the current time the query is executed
My question is: can I do date subtraction from within the query? Do operators exist so I can directly type the timeout inside the query? In SQL, at least with MySQL, I was able to do date subtraction with the NOW() function. Is it possible in ADO.NET too? Kepp in mind that this is an in-memory table for offline ADO.NET since data is not meant to be persisted in any way.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要问的是是否可以在查询字符串中进行日期算术,那么不行;
DataTable
和DataView
中提供的过滤和查询功能都非常简单。根据您正在执行的操作,您可能最好使用 LINQ 查询。If what you're asking is if you can do date arithmetic in the query string, then no; the filtering and query functionality available in the
DataTable
andDataView
are both very simple. Depending on what you're doing you may be better off using a LINQ query instead.