我有个程序,运行完后要更新记录,记录格式如:
xxx + 存档时间
yyy + 存档时间
zzz + 存档时间更新规则为
1.先遍历记录,发现有哪条记录超过一天就直接删掉它。
2.把新的记录写进去。发现有两个问题难着我,
1.用什么样的时间格式存储好呢?
2.怎么判断现在的时间-存档时间 > 1天呢?最少要精确到秒。因为每年的月、日都不同,所以这个问题比较麻烦,凭现在的我用TimeSpan+DateTime好像解决不了问题。请大家帮帮我!
xxx + 存档时间
yyy + 存档时间
zzz + 存档时间更新规则为
1.先遍历记录,发现有哪条记录超过一天就直接删掉它。
2.把新的记录写进去。发现有两个问题难着我,
1.用什么样的时间格式存储好呢?
2.怎么判断现在的时间-存档时间 > 1天呢?最少要精确到秒。因为每年的月、日都不同,所以这个问题比较麻烦,凭现在的我用TimeSpan+DateTime好像解决不了问题。请大家帮帮我!
为什么解决不了问题呢?精度至少是在毫秒级别。
@name varchar(20) --传入参数管理员姓名,即你提到的xxx,yyy,zzz
as
declare cur cursor --定义游标,遍历所有项 ,假定表名为table1,三列列名分别为nid, name 和 time
for select nid,[time] from table1
declare @time datetime
declare @nid int
open cur
fetch next from cur into @nid,@time
While(@@Fetch_Status =0)
begin
if DATEDIFF(day,@time, getdate())>0 --判断,表中的时间与当前系统时间做比较,此处需注意,是大于0而不是大于1
--delete table1 where [time]=@time --删除原数据
--insert into table1 values('aaa',getdate()) --插入新数据
update table1 set [name]='xxx', [time]=getdate() where nid=@nid
fetch next from cur into @nid,@time
end
Close cur
Deallocate curgo
楼主:我认为你简单点,程序内部有计算时间转换的方法。 你直接用dt.Now.ToString("yyyyMMddhhmmss"); 这样也好计算。
关键是转化成string后如何转换回来做比较运算?这个你就放在一个统一转换的方法里。这只是要您写一个公共方法而已
如果有需要的话,你最好把毫秒也放进去。
DateTime dt = DateTime.Now; TimeSpan ts;
ts = dt - DateTime.ParseExact(时间串,"yyyyMMddHHmmss", null);