获得总时间和超时的正确计算是什么 [英] What is the right computation for getting the total hours of time in and time out

查看:92
本文介绍了获得总时间和超时的正确计算是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你需要帮助人员尽快...我需要获得datagridview中列出的确切总小时数,一旦我点击填充按钮... datagridview列标题名称是日期,状态,时间

值是





date = 22/03/2018 date = 22/03/2018

status = in status = out

time = 7:30 time = 14:00



date = 23/03/2018 date = 22 / 03/2018

status = in status = out

time = 6:45 time = 17:00



两天内的总小时数总是16 ...你可以看到她在下午2点出门,然后在早上晚了30分钟..



他们时间的政策

是早上7点到下午5点工作= 8小时工作2小时是breaktym(不计算在内)

早上渲染时间= 3.5

下午渲染时间= 4.5

总计= 8小时





我怎么能获得员工的确切工作时间?我认为这个代码有错误。



我尝试过:



如果txtID.Text =那么
MsgBox(插入员工ID)

ElseIf txtID.Text<> 然后
尝试
totalMins = 0
conn.Open()
query = New MySqlCommand(SELECT No,Name,date,status,position,TIME_FORMAT(STR_TO_DATE(time) ,'%l:%i:%s%p'),'%H:%i%p')AS时间,HOUR(TIME_FORMAT(时间,'%H:%i'))AS小时,MINUTE(TIME_FORMAT(时间,'%H:%i'))AS分钟来自cash_table WHERE否=& txtID.Text&AND(date BETWEEN'& DateTimePicker2.Value.Date&'AND'& DateTimePicker3 .Value.Date&'),conn)
dr = query.ExecuteReader()
如果dr.HasRows那么
Do while dr.Read()
'控制台.WriteLine(dr.GetInt32(0)& vbTab& dr.GetString(1))
txtName.Text = dr.GetString(Name)
txtPosition.Text = dr.GetString(位置)

arr(0)= dr.Item(date)
arr(1)= dr.Item(status)
' arr(2)= dr.Item(position)
arr(2)= dr.Item(time)
DataGridView1.Rows.Add(arr)

stat = dr.Item(status)
timeHr = dr.Item(hour)
timeMin = dr.Item(minute)

If(stat = IN)然后
lateMin = 0
extMin = 0

If(timeHr> 7)然后
lateMin + =(timeHr - 7)* 60

结束如果

如果(timeHr> = 7)那么
If( timeMin> 0)然后
lateMin + =(timeMin)
结束如果
结束如果
ElseIf(stat =OUT)那么
If(timeHr> 17)然后
extMin + =(timeHr - 17)* 60
结束如果

如果(timeHr> = 17)那么
If(timeMin> = 15)然后
extMin + =(timeMin)
结束如果
结束如果

如果(timeHr> 17)那么
extMin + =(3.5 - (timeHr - 13))* 60 + timeMin - 16

结束如果
totalMins + =(480 - lateMin)+(extMin)
totalMins = Val(totalMins) - Val(16)
End If
Loop
Else
MsgBox(No Records Found!,MsgBoxStyle.Information,Search)
End if
dr.Dispose()

TextBox4.Text = Math.Floor (totalMins / 60)& :& (totalMins Mod 60)

Catch ex As Exception
MsgBox(ex.Message)

最后
dr.Dispose()
conn .Close()

结束尝试

如果txtID.Text =那么
DataGridView1.Rows.Clear()
txtName.Clear()
txtPosition.Clear()
结束如果

结束如果

解决方案

您在该代码中确实存在错误:永远不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



这不是你注意到的问题,但它需要在整个应用程序中修复,然后再继续。 ..我们不知道你注意到的问题是什么。所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


引用:

我认为这个代码有错误。



您不期望发生什么?

-----

 query =  MySqlCommand(  SELECT No,Name,date,status,position,TIME_FORMAT(STR_TO_DATE(time,'%l:%i:%s%p'),'%H :%i%p')AS时间,HOUR(TIME_FORMAT(时间,'%H:%i'))AS小时,MINUTE(TIME_FORMAT(时间,'%H:%i'))AS分钟来自cash_table WHERE否= & txtID.Text&   AND(date BETWEEN'& DateTimePicker2 .Value。日期&  'AND'& DateTimePicker3.Value。日期&  '),conn)



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Hi need help guys ASAP.. i need to get the exact total number of hours listed in datagridview once i click the fill button ... the datagridview column header name is date,status, time
the values are


date=22/03/2018 date=22/03/2018
status= in status= out
time=7:30 time= 14:00

date=23/03/2018 date=22/03/2018
status=in status=out
time= 6:45 time = 17:00

the total number of hrs in two days always 16.. as you can see she was out at 2 oclock pm then have a 30 min late on morning..

the policy for their time
is 7am-5pm hours work = 8 hrs of work 2 hours are breaktym (not counted)
morning rendered time = 3.5
afternoon rendered time = 4.5
total = 8 hrs
.
.
how can i get the exact total time of work of employee ?i think a have an error of this code.

What I have tried:

If txtID.Text = "" Then
        MsgBox("insert Employee ID")

    ElseIf txtID.Text <> "" Then
        Try
            totalMins = 0
            conn.Open()
            query = New MySqlCommand("SELECT No, Name, date, status, position,TIME_FORMAT(STR_TO_DATE(time,  '%l:%i:%s %p'), '%H:%i %p') AS time,HOUR(TIME_FORMAT(time, '%H:%i ')) AS hour,MINUTE(TIME_FORMAT(time,'%H:%i')) AS minute From cash_table WHERE No=" & txtID.Text & " AND (date BETWEEN '" & DateTimePicker2.Value.Date & "' AND '" & DateTimePicker3.Value.Date & "')", conn)
            dr = query.ExecuteReader()
            If dr.HasRows Then
                Do While dr.Read()
                    'Console.WriteLine(dr.GetInt32(0) & vbTab & dr.GetString(1))
                    txtName.Text = dr.GetString("Name")
                    txtPosition.Text = dr.GetString("position")

                    arr(0) = dr.Item("date")
                    arr(1) = dr.Item("status")
                    'arr(2) = dr.Item("position")
                    arr(2) = dr.Item("time")
                    DataGridView1.Rows.Add(arr)

                    stat = dr.Item("status")
                    timeHr = dr.Item("hour")
                    timeMin = dr.Item("minute")

                    If (stat = "IN") Then
                        lateMin = 0
                        extMin = 0

                        If (timeHr > 7) Then
                            lateMin += (timeHr - 7) * 60

                        End If

                        If (timeHr >= 7) Then
                            If (timeMin > 0) Then
                                lateMin += (timeMin)
                            End If
                        End If
                    ElseIf (stat = "OUT") Then
                        If (timeHr > 17) Then
                            extMin += (timeHr - 17) * 60
                        End If

                        If (timeHr >= 17) Then
                            If (timeMin >= 15) Then
                                extMin += (timeMin)
                            End If
                        End If

                        If (timeHr > 17) Then
                            extMin += (3.5 - (timeHr - 13)) * 60 + timeMin - 16

                        End If
                        totalMins += (480 - lateMin) + (extMin)
                        totalMins = Val(totalMins) - Val(16)
                    End If
                Loop
            Else
                MsgBox("No Records Found!", MsgBoxStyle.Information, "Search")
            End If
            dr.Dispose()

            TextBox4.Text = Math.Floor(totalMins / 60) & ":" & (totalMins Mod 60)

        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            dr.Dispose()
            conn.Close()

        End Try

        If txtID.Text = "" Then
            DataGridView1.Rows.Clear()
            txtName.Clear()
            txtPosition.Clear()
        End If

    End If

解决方案

You do have errors in that code: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

That's not the problem you've noticed, but it needs fixing throughout your whole app before you go any further ... and we have no idea what the problem you have noticed is. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


Quote:

i think a have an error of this code.


What happen that you do not expect ?
-----

query = New MySqlCommand("SELECT No, Name, date, status, position,TIME_FORMAT(STR_TO_DATE(time,  '%l:%i:%s %p'), '%H:%i %p') AS time,HOUR(TIME_FORMAT(time, '%H:%i ')) AS hour,MINUTE(TIME_FORMAT(time,'%H:%i')) AS minute From cash_table WHERE No=" & txtID.Text & " AND (date BETWEEN '" & DateTimePicker2.Value.Date & "' AND '" & DateTimePicker3.Value.Date & "')", conn)


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于获得总时间和超时的正确计算是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆