从vb.net中的字符串转换日期和/或时间。获得两次差异然后更新表格

时间:2017-10-14 10:52:07

标签: sql vb.net datediff

这是我的代码:

Dim time_in As DateTime = Nothing
    Dim time_out As DateTime = Nothing
    Dim totalWorkHours As String = Nothing

    getQuery = "SELECT dtr.time_in, dtr.time_out FROM dtr, worker WHERE dtr.worker_id ='" & getWorkerID & "' AND worker.worker_id ='" & getWorkerID & "' AND dtr.time_out IS NOT NULL"
    getCommand = New SqlCommand(getQuery, cn)
    cn.Open()
    getReader = getCommand.ExecuteReader

    While getReader.Read

        time_in = (getReader.Item("time_in").ToString())
        time_out = (getReader.Item("time_out").ToString())

        totalWorkHours = DateDiff(DateInterval.Second, time_in, time_out) / 3600.0

    End While

    cn.Close()
    getReader.Close()

    getQuery = "UPDATE dtr SET dtr.hours_work ='" & totalWorkHours & "' WHERE dtr.worker_id ='" & getWorkerID & "' AND dtr.date ='" & Format(Date.Today, "yyyy-MM-dd") & "'"
    getCommand = New SqlCommand(getQuery, cn)
    cn.Open()
    getReader = getCommand.ExecuteReader
    cn.Close()
    getReader.Close()

我在更新表时遇到未处理的异常。我在我的代码中工作的是通过两次hours_worktime_in的差异得到总time_out

这是我的表dtr

CREATE TABLE [dbo].[dtr] (
    [worker_id]  INT      DEFAULT (NULL) NULL,
    [time_in]    DATETIME DEFAULT (NULL) NULL,
    [time_out]   DATETIME DEFAULT (NULL) NULL,
    [date]       DATE     DEFAULT (NULL) NULL,
    [hours_work] DATETIME DEFAULT (NULL) NULL
);

1 个答案:

答案 0 :(得分:0)

所以我在数据库中使用的数据类型错了。

我用过 [hours_work] DATETIME DEFAULT (NULL) NULL
而不是[hours_work] DECIMAL(18, 2) NULL

问题解决了。谢谢@jmcilhinney给我答案。 我在totalWorkHours上做了什么我把它改成了两倍但仍然不起作用。所以我把它改回字符串,然后尝试更改数据库中的数据类型。再次感谢你:D