c# - 检查时间(考勤 c#)

标签 c# mysql time-and-attendance

我正在制作一个小程序,员工可以在其中上下类。 员工有一个员工 ID,必须使用该 ID 才能上下类。

计时没问题,但我的问题是员工可以多次计时而不会超时。

我想做的是,每当员工计时时,他/她就不允许再次计时,除非他/她超时。

这是我到目前为止所做的事情:

private void TimeIN()
    {
        //check if Employee ID belongs to the tbl_humans table

        Connection();
        sql_connect.Close();
        sql_connect.Open();
        sql_command = new MySqlCommand("Select * from tbl_humans where ID = '" + textBox1.Text + "' ;", sql_connect);

        sql_reader = sql_command.ExecuteReader();

        string username;
        //string password;

        username = (textBox1.Text);
        // password = (textBox2.Text);

        int count = 0;

        while (sql_reader.Read())
        {
            count = count + 1;
        }


        //checks if the Employee ID has been already timed in on tbl_loginout
        // if it's already timed in, it will display "You time out first"
        // but unfortunately, it's not working and still continues to time in the employee even if the employee is already timed in

        if (count == 0) 
        {
           try
           {
               Connection();
               sql_connect.Close();
               sql_connect.Open();


               sql_command = new MySqlCommand
                   ("Select * from tbl_loginout where ID = '" + textBox1.Text +
                   "' and timein_date = '" + DateTime.Today.ToString() + // checks if the employee is timed in on that day(today)
                   "' and timeout_time = '" + DateTime.Now.ToString("00:00:00") + // checks if the employee is still not time out
                   "';", sql_connect);
               sql_reader = sql_command.ExecuteReader();

               MessageBox.Show("You Time Out first");
           }

           catch (Exception aa)
           {
               MessageBox.Show(aa.Message);
           }
       }

如果有帮助,我们将不胜感激。谢谢!

最佳答案

尝试从查询中删除 "' 和 timeout_time = '"+ DateTime.Now.ToString("00:00:00") + "';" 或将其替换为 "' 且 timeout_time = null;"

一般来说,您应该手动编写想要使用的 SQL 并直接执行它,以确保它返回您想要的结果。

还有一点评论:您执行读取器sql_reader = sql_command.ExecuteReader();并且不对结果执行任何操作,无论实际结果是什么,您都只是显示消息框。

关于c# - 检查时间(考勤 c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34790613/

相关文章:

c# - 如何在 Xamarin iOS 项目中使用签名程序集 (PCL)?

c# - 更改 XML 序列化中的元素类型

mysql - 如何给出多于一个的having子句

mysql - 优化大型 MySQL 表 - 分区?

c# - SELECT 过滤器查询中的日期格式

c# - 为什么 Roslyn 不会抛出方法统一错误?

c# - 使用 MVC Core Identity 从 Web API 验证 MVC 网站用户

PostgreSQL - 将时间与值进行比较 >24 :00

php - 在 PHP 和 MySQL 中确定和存储打卡时间的最佳方法