如何将存储在Access 2007中的两个时间值减去为text数据类型 [英] How to subtract two time values which is stored in access 2007 as a text datatype
本文介绍了如何将存储在Access 2007中的两个时间值减去为text数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
How to subtract two time values which is stored in access 2007 as a text datatype
我尝试过:
What I have tried:
String str1 = null;
string str2 = null;
con1.Open();
cmd1.CommandText = "insert into TimeCalculation (AttendanceLogId,AttendanceDate,EmployeeId,InTime,OutTime,Duration) select distinct AttendanceLogId,AttendanceDate,EmployeeId,InTime,OutTime,@d from AttendanceLogs where NOT EXISTS ( select * from TimeCalculation where TimeCalculation.AttendanceLogId = AttendanceLogs.AttendanceLogId )";
OleDbDataReader rd = cmd1.ExecuteReader();
while (rd.Read())
{
str1 = (rd["InTime"].ToString());
str2 = (rd["OutTime"].ToString());
}
TimeSpan time = DateTime.Parse(str2).Subtract(DateTime.Parse(str1));
cmd1.Parameters.AddWithValue("@d", time.ToString());
推荐答案
简单:不要。
将它们存储为DateTime而不是字符串,并且你可以立即使用标准的比较和数学运算 - 使一切变得更加容易。
始终以相关的数据类型存储数据:其他任何东西都会在以后给你带来问题这比使用正确类型的琐碎麻烦要大得多。
Simple: Don't.
Store them as DateTime instead of strings, and you can immediately use standard comparison and math operations - making everything a whole load easier.
Always store data in relevant datatypes: anything else will always give you problems later on which are a huge amount worse than the trivial hassle it is to use the right types in the first place.
这篇关于如何将存储在Access 2007中的两个时间值减去为text数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文