RowFilter日期筛选请教~!

gooodiuck 2012-04-28 11:00:11
数据字段类型是“yyyy-MM-dd HH:mm:ss”我想筛选出日期从DAY1天到DAY2天,并且时间在TIME1到TIME2之间的所有数据
例:日期是从2012-04-01至2012-04-10并且时间为08:00:00至09:00:00 的所有数据。请问RowFilter的筛选语句如何写?
...全文
509 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
gooodiuck 2012-05-03
  • 打赏
  • 举报
回复
再次感谢“Chinajiyong”,也感谢微软论坛的“编程志愿者”
gooodiuck 2012-05-03
  • 打赏
  • 举报
回复
在高手指点下,问题得以解决。
方法有二:

//方法一,NET3.5中用LINQ
namespace CSharp
{
public class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("DateTimeNow",typeof(DateTime));
DateTime td = new DateTime(2012,4,1,7,0,0);
Random r = new Random(Guid.NewGuid().GetHashCode());
for (int i = 1; i < 21; i++)
{
dt.Rows.Add(td);
td=td.AddDays(1);
td=td.AddHours(r.Next(1,3));
Thread.Sleep(5);
//输出原来的日期
Console.WriteLine(dt.Rows[i-1][0]);
}
Console.WriteLine("============");

var result = from row in dt.AsEnumerable()
let date = DateTime.Parse(row[0].ToString()).Date
let time = DateTime.Parse(row[0].ToString()).TimeOfDay
where date >= new DateTime(2012, 4, 1) && date <= new DateTime(2012, 4, 10) &&
time >= new TimeSpan(8, 0, 0) && time <= new TimeSpan(9, 0, 0)
select row;

foreach (var item in result)
{
Console.WriteLine(item[0].ToString());
}
}
}
}
//方法二
namespace MyTest
{
public class TestArray
{
public static void Main()
{
DataTable dt = new DataTable();
dt.Columns.Add("DateTimeNow", typeof(DateTime));
DataTable cdt = dt.Clone(); //拷贝副本
DateTime td = new DateTime(2012, 4, 1, 7, 0, 0);
Random r = new Random(Guid.NewGuid().GetHashCode());
for (int i = 1; i < 21; i++)
{
dt.Rows.Add(td);
td = td.AddDays(1);
td = td.AddHours(r.Next(1, 3));
Thread.Sleep(5);
//输出原来的日期
Console.WriteLine(dt.Rows[i - 1][0]);
}
Console.WriteLine("============");

//先过滤日期
DataView dv = new DataView(dt);
dv.RowFilter = "DateTimeNow>='2012/04/01 00:00:00' And DateTimeNow<='2012/04/10 23:59:59'";
dt = dv.ToTable();

//再过滤时间
foreach (DataRow item in dt.Rows)
{
DateTime dtt = DateTime.Parse(item[0].ToString());
if (dtt.TimeOfDay >= new TimeSpan(8, 0, 0) && dtt.TimeOfDay <= new TimeSpan(9, 0, 0))
{
cdt.Rows.Add(dtt);
}
}

//输出
foreach (DataRow item in cdt.Rows)
{
Console.WriteLine(item[0].ToString());
}
}
}
}

Mirror然 2012-05-02
  • 打赏
  • 举报
回复
好纠结的人儿。。。
SQL能处理的问题 非要搬到代码里面去执行
按照你这需求 还不如弄个存储过程
EnForGrass 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

RowFilter对Convert倒是支持的。
[/Quote]
你试试,SELECT * FROM testDate WHERE (SUBSTRING(CONVERT(VARCHAR,DateLoan,120),1,10) >='2012-04-27' 看看这个能不能成功?
gooodiuck 2012-05-02
  • 打赏
  • 举报
回复
RowFilter对Convert倒是支持的。
EnForGrass 2012-05-02
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

谢谢Chinajiyong的解答,但我还是需要通过筛选来获取数据。
[/Quote]
我试过貌似RowFilter不支持Convert等函数
gooodiuck 2012-05-02
  • 打赏
  • 举报
回复
谢谢Chinajiyong的解答,但我还是需要通过筛选来获取数据。
gooodiuck 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

你先判断日期合法性 然后判断hour在不在8-9之间不就行了吗?
[/Quote]
晕!我也是这么想的。
但我想知道如何代码如何写!
jzz_123 2012-04-28
  • 打赏
  • 举报
回复
你先判断日期合法性 然后判断hour在不在8-9之间不就行了吗?
gooodiuck 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

2012-04-01 08:00:00到2012-04-10 09:00:00不同吗?
dv.RowFilter="时间字段 between '2012-04-01 08:00:0' and '2012-04-10 09:00:00'"
[/Quote]

我的意思是:记录是从4-1日到4-10日,并且时间段限于8点至9点之间的记录。小于8点,大于9点,虽然是在4-1到4-10之间,但仍被剔出!
EnForGrass 2012-04-28
  • 打赏
  • 举报
回复
2012-04-01 08:00:00到2012-04-10 09:00:00不同吗?
dv.RowFilter="时间字段 between '2012-04-01 08:00:0' and '2012-04-10 09:00:00'"
EnForGrass 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

呵呵,你说的不错,你这种写法,是SQL写法,但是用在ROWFILTER里面就不对了。
[/Quote]
那直接把sql语句写成上面那个不行吗?
这样

DataSet dateds = new DataSet();
string constring = @"server=YKZ-20110406NYO\SQLEXPRESS;DataBase=master;User ID=sa;Password=abc12345";
SqlConnection conn = new SqlConnection(constring);
string sql = @"SELECT * FROM testDate WHERE (SUBSTRING(CONVERT(VARCHAR,DateLoan,120),1,10) >='2012-04-27' and SUBSTRING(CONVERT(VARCHAR,DateLoan,120),1,10)<='2012-04-28') and (SUBSTRING(CONVERT(VARCHAR,DateLoan,114),1,8) >= '08:00:00' and SUBSTRING(CONVERT(VARCHAR,DateLoan,114),1,8)<='09:00:00')
";
SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
adpter.Fill(dateds, "testDate");
DataTable dtdate = dateds.Tables["testDate"];
KaddyChan 2012-04-28
  • 打赏
  • 举报
回复
Chinajiyong 大大要怒了
gooodiuck 2012-04-28
  • 打赏
  • 举报
回复
呵呵,你说的不错,你这种写法,是SQL写法,但是用在ROWFILTER里面就不对了。
EnForGrass 2012-04-28
  • 打赏
  • 举报
回复

create table testDate
(
ID int primary key identity(1,1),
DateLoan datetime
)
insert into testDate values('2012-4-27 07:20:10')
insert into testDate values('2012-4-28 07:20:10')
insert into testDate values('2012-4-28 08:20:10')
insert into testDate values('2012-4-28 08:30:10')
insert into testDate values('2012-4-28 08:40:10')
insert into testDate values('2012-4-28 08:50:10')
insert into testDate values('2012-4-28 09:00:00')
insert into testDate values('2012-4-28 09:20:10')
insert into testDate values('2012-4-28 09:20:10')

SELECT * FROM testDate
/*
ID DateLoan
----------- -----------------------
1 2012-04-28 07:20:10.000
2 2012-04-28 08:20:10.000
3 2012-04-28 08:30:10.000
4 2012-04-28 08:40:10.000
5 2012-04-28 08:50:10.000
6 2012-04-28 09:00:00.000
7 2012-04-28 09:20:10.000
8 2012-04-28 09:20:10.000
9 2012-04-27 07:20:10.000

(9 行受影响)
*/
SELECT * FROM testDate WHERE SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,120),1,10) between '2012-04-27' and '2012-04-28' and SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,114),1,8) between '08:00:00' and '09:00:00'
/*
ID DateLoan
----------- -----------------------
2 2012-04-28 08:20:10.000
3 2012-04-28 08:30:10.000
4 2012-04-28 08:40:10.000
5 2012-04-28 08:50:10.000
6 2012-04-28 09:00:00.000

(5 行受影响)
*/

在C#代码里这样写
dv.RowFilter=" SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,120),1,10) between '2012-04-27' and '2012-04-28' and SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,114),1,8) between '08:00:00' and '09:00:00'";
gooodiuck 2012-04-28
  • 打赏
  • 举报
回复
谢谢“chinajiyong” 我运行,下提示,不支持CONVERT,不支持BETWEEN
EnForGrass 2012-04-28
  • 打赏
  • 举报
回复
dv.RowFilter=" SUBSTRING(CONVERT(NVARCHAR(20),时间字段,120),1,10) between '2012-04-01 08:00:00' and '2012-04-10 09:00:00' and SUBSTRING(CONVERT(NVARCHAR(20),GETDATE(),114),1,8) between '08:00:00' and '09:00:00'"

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧