循环通过记录获得总数

时间:2015-06-30 18:35:39

标签: sql asp.net vb.net loops recordset

我有一个网页,我想显示使用visual basic拍摄的总休假时间。所有日期都存储在SQL数据库中。

我想让它获取tblworkhours中的所有记录,这些记录是员工登录到计算机并且工作代码为2的假期,然后我将计算总休假时间。

我把它设置为计算表中的所有记录(工作正常)。然后我找出该记录中的休息时间(工作正常)。我的问题 - 在我完成从该记录中获取信息后,我想转到下一条记录,让它休息几小时,然后是下一小时,依此类推,直到我为该员工休息一小时。我无法弄清楚如何让它去下一个记录,然后是下一个记录,依此类推,直到我到达记录的末尾。

示例 - 我的选择计数语句显示我有2条记录,其中员工是谁登录,工作代码是休假(2)。

第1条记录 - 2015年6月13日开始日期 - 2015年6月15日结束日期 - 所以,我计算3天或24小时

第2条记录 - 2015年6月20日开始日期 - 2015年6月20日结束日期 - 1天或8小时

我的代码计算了多少条记录 - 2. Do Until语句工作正常 - 它计算1和2,但它计算第一条记录2x。而不是总共32小时(第1小时加上第2小时),我得到48小时(第1小时2倍)。

仅供参考 - 我在增加时间方面没有问题。我的代码必须计算小时数,因为该表只有日期和时间。 在此先感谢您的帮助!

这是我的代码 - 我保留了正常工作的部分:

    Dim sqlConnection As New SqlConnection("Data Source=WillSQL\ict2;Initial Catalog=TimeSQL;Integrated Security=SSPI")

    Dim commandvct As New SqlCommand
    Dim returnvct As Object

    commandvct.CommandText = "Select count(workhoursid) from tblworkhours where Employee = " & rve & " and workcode = 2" 
'workcode 2 is vacation.  Count works fine.  
    commandvct.CommandType = CommandType.Text
    commandvct.Connection = sqlConnection

    sqlConnection.Open()
    returnvct = commandvct.ExecuteScalar
    sqlConnection.Close()

    Do Until returnvct = 0

        returnvct = returnvct - 1
        ' Deleted code that gets how many hours - this code works fine.

    Loop

2 个答案:

答案 0 :(得分:1)

让我展示一下使用Sql语言可以做些什么。在本例中,我将仅使用SQL总结休假时间

create table A (name varchar(100), bdate datetime, edate datetime) 

-- I set dates to string, it will convert to rounded dates.
insert into a values ('AAA', '02/05/2015', '02/05/2015') -- 1 day / 8 hrs
insert into a values ('AAA', '06/05/2015', '06/09/2015') -- 5 days / 40 hrs 
insert into a values ('BBB', '02/05/2014', '02/05/2014') -- 1 day / 8 hrs
insert into a values ('BBB', '06/05/2014', '06/07/2014') -- 3 days / 24 hrs

-- But when taking dates out, you need to cut off hours, minutes, etc - use CAST
select name, SUM(case 
                     when CAST(bdate as date) = CAST(edate as date) Then 8
                     else (datediff(day, CAST(bdate as date), CAST(edate as date)) + 1) * 8 -- +1 to make date inclusive
                 end) as vacaHours
from a
where name = 'AAA' -- comment this and all people will return
group by name

AAA

的输出

name vacaHours AAA 48

所有

的输出

name vacaHours AAA 48 BBB 32

现在,您可以打开DataReader并将值读入程序

' They said, sql injection words... this is what you do - notice alias for count
cmd.CommandText = "Select count(workhoursid) as count from tblworkhours where Employee = @1 and workcode = @2" 
cmd.Parameters.AddWithValue("@1", rve)
cmd.Parameters.AddWithValue("@2", 2)
' some code here
cmd.CommandType = CommandType.Text
' some code here

Using reader As IDataReader = cmd.ExecuteReader()
    ' do something with each record
    While reader.Read()

        myvar = Convert.ToInt32(reader("count"))

    End While

End Using

答案 1 :(得分:1)

您只有一条基于此代码中的SQL查询的记录。没有第二条记录。

查询返回一个记录,其中包含符合条件的所有记录的计数。并且您正确使用ExecuteScalar方法来获取该值。

除了每次从返回的整数中减去一个之外,还不清楚你在最后循环的是什么。你肯定没有遍历任何数据记录。

如果您需要所有记录,请选择所有记录。

SELECT * FROM tblworkhours where Employee = " & rve & " and workcode = 2

然后使用它返回一个datareader并遍历那里的记录。

有人提到你应该使用参数。这是真的,但可能会分散你的主要问题。

相关问题