使用 Dataview Rowfilter 根据时间过滤记录
是否可以使用属于表的 Defaultview 的 RowFilter 属性从 DataTable 获取时间跨度之间的记录。
目的: 我想检查当前时间(DateTime.Now.ToString()) 是否符合数据库表架构中定义的时间。表的架构定义如下
`Name | From | To | JoinedOn |`
,其中 From To 是数据库表上的日期时间列。我只想比较日期时间列中的时间值,而不是日期。我在下面尝试过,但没有产生结果。 dt_Temp.DefaultView.RowFilter = "从 < '"+ DateTime.Now.ToString()+"' AND 到 >'"+ DateTime.Now.ToString()+"'";
任何人有什么想法吗?
规格:
.Net Framework 2.0
ViSual studio 2005
Sql server express 2005
Is it possible to get records between a Timespan from a DataTable using the RowFilter property of the Defaultview that belongs to the table.
Purpose:
I would like to check if Current Time(DateTime.Now.ToString())
falls withing the Time defined in the Database table schema. Schema of the table is defined below
`Name | From | To | JoinedOn |`
where From To are datetime columns on the database table. I would like to compare only time values from the datetime columns rather than the dates also. I Tried below but doesn't yield the result.dt_Temp.DefaultView.RowFilter = "From < '"+ DateTime.Now.ToString()+"' AND To >'"+ DateTime.Now.ToString()+"'";
anyone have any idea?
Specs:
.Net Framework 2.0
ViSual studio 2005
Sql server express 2005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会跳过 DefaultView 并针对 DataTable 编写 Linq 查询,而不是
编辑
这是该查询的 2.0 兼容函数
I would skip the DefaultView and write a Linq query agains the DataTable instead
Edit
Here is a 2.0 compatible function of that query
对我自己的智慧感到惊讶:)下面的 rowfilter 回答了我的查询
"From <= #" + DateTime.Now.ToString() + "# AND To >#" + DateTime.Now.ToString( ) + "#";
Astonished by my own intelligence :) the below rowfilter answer my query
"From <= #" + DateTime.Now.ToString() + "# AND To >#" + DateTime.Now.ToString() + "#";