SELECT
t1.empid,
t1.department,
YEAR(t1.timestamp) Year,
MONTH(t1.timestamp) Month,
CONVERT(
varchar(12),
DATEADD(minute, SUM(DATEDIFF(minute, t1.timestamp, t2.timestamp)), 0),
114
) TotalHours
FROM
mytable t1
INNER JOIN mytable t2
ON t1.empid = t2.empid
AND t2.read_id = 2
AND t2.timestamp = (
SELECT MIN(timestamp)
FROM mytable
WHERE
read_id = 2
AND empid = t2.empid
AND timestamp > t1.timestamp
)
WHERE
t1.read_id = 1
AND NOT EXISTS (
SELECT 1
FROM mytable
WHERE
read_id = 1
AND empid = t1.empid
AND timestamp > t1.timestamp
AND timestamp < t2.timestamp
)
GROUP BY t1.empid, t1.department, YEAR(t1.timestamp), MONTH(t1.timestamp)
ORDER BY 1, 2, 3, 4
我将当前设置文件重命名为.bak文件,然后替换当前设置文件。我这样做是因为如果用户在下载过程中关闭电源,我的文件可能/将损坏。我认为,如果设置文件已损坏,则可以从备份中读取该设计模式,但备份文件也将损坏。
答案 0 :(得分:0)
写入文件时,数据进入OS回写缓存。当操作系统意外崩溃(断电,蓝屏,VM中断)时,该数据将丢失。典型的症状是文件具有正确的大小,但全为零。 (也许大小也不对,我不知道。)
通常,写一个新文件然后交换新旧文件的想法是一个不错的策略。但是您还需要将数据刷新到磁盘。
FileStream.Flush()
和StreamWriter.Flush()
做到这一点。因此,您应该添加f.Flush();
。