【问题标题】:i need to get the time work for each employee [closed]我需要为每个员工争取时间[关闭]
【发布时间】:2017-08-14 21:40:52
【问题描述】:

我有一个访问数据库,存储每个员工上班的时间和员工离开的时间。我想计算用户在家工作的时间。这是我的代码:

    DataTable dm = new DataTable();
    today = DateTime.Today;
    hooodorDa = new OleDbDataAdapter("select heUsers,heCome,heGo from HoodoorEnseraf where heDate=#" + today.ToString("yyyy/MM/dd") + "# and  heUsers='" + heUsers.Text + "' ", connection);
    hooodorDa.Fill(dm);
    DateTime hec = Convert.ToDateTime(dm.Rows[0]["heCome"]);
    DateTime heg = Convert.ToDateTime(dm.Rows[0]["heGo"]);
    TimeSpan diff = hec - heg;
    command = new OleDbCommand("update  HoodoorEnseraf set heDifference=? where heUsers=? ;", connection);
    command.Parameters.AddWithValue("?", diff);
    command.Parameters.AddWithValue("?", heUsers.Text);

我需要在数字字段中插入值,如下面的屏幕截图所示,但我的代码给了我一个错误:

对象不能从 dbnull 转换为其他类型

我的代码有什么问题?

【问题讨论】:

  • 你的问题是什么?我的意思是,很高兴你把代码放在这里你到目前为止尝试过的东西,但是用你发布的代码解释你的问题可能会给你的问题带来更多的回应
  • 那里有什么问题? diff 值是否正确?
  • 不,我收到此错误(对象无法从 dbnull 转换为其他类型

标签: c# ms-access error-handling


【解决方案1】:

您似乎正在寻找TimeSpan.TotalMinutes 属性。您的heDifference 是一个数字,因此您可以获得用户工作的总分钟数并将其存储在那里。 您可以使用TimeSpan.TotalHours,但分钟更准确。



更正的代码:

DataTable dm = new DataTable();
today = DateTime.Today;
hooodorDa = new OleDbDataAdapter("select heUsers,heCome,heGo from HoodoorEnseraf where heDate=#" + today.ToString("yyyy/MM/dd") + "# and  heUsers='" + heUsers.Text + "' ", connection);
hooodorDa.Fill(dm);
DateTime hec = dm.Rows[0].Field<DateTime>("heCome");
DateTime heg = dm.Rows[0].Field<DateTime>("heGo");
TimeSpan diff = hec - heg;
command = new OleDbCommand("update  HoodoorEnseraf set heDifference=" +
    diff.TotalMinutes.ToString() + " where heUsers='" + heUsers.Text + "';", connection);

【讨论】:

  • “但是分钟更准确” - 这是为什么呢?
  • @Sinatr OP 想知道员工工作了多长时间 - 因此分钟可以更准确地表示他们在给定一天工作的时间量。如果需要,秒会更准确,尽管分钟也可以。
  • 两个属性都是double。为什么1.5 小时的准确度不如90.0 分钟?
  • @Sinatr 我不是说属性——DB字段是一个整数,所以它不能容纳1.5
  • 如何在我的代码中使用它请帮忙
猜你喜欢
  • 2021-12-14
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多