使用qr代码C#winform的出勤申请 [英] Attendance application using qr code C# winform

查看:110
本文介绍了使用qr代码C#winform的出勤申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的表单中创建了一个计时器,它在单击相机按钮时启动。我想检查用户是否有过期的ID,并在(checkin)表中插入ID以下是我的代码到目前为止,请审核并提供反馈。



我尝试过:



I have created a timer in my form, it starts when camera button is click. I want to check if the user have expired ID or not and insert the ID in (checkin) table Given below is my code so far, kindly review and give feedback.

What I have tried:

private void Timer1_Tick(object sender, EventArgs e)
        {
            BarcodeReader Reader = new BarcodeReader();
            Result result = Reader.Decode((Bitmap)pictureBox1.Image);
            try
            {
                string decoded = result.ToString().Trim();
                if (decoded != "")
                {
                    timer1.Stop();
                    MessageBox.Show(decoded);

                    using (SqlConnection con = new SqlConnection("Data Source=SQL5037.site4now.net;Initial Catalog=DB_A448D1_Dragon;User Id=***********;Password=******"))
                    {
                        con.Open();
                        try
                        {
                            using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                            {

                                com.Parameters.AddWithValue("@ID", decoded.Trim());
                                com.Parameters.AddWithValue("@C1", DateTime.Now);
                                com.Parameters.AddWithValue("@C2", DateTime.Now);
                                
                                int count = (int)com.ExecuteScalar();
                                if (count > 0)
                                {
                                    using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time) VALUES (@ID,@time)", con))
                                    {
                                        com1.Parameters.AddWithValue("@ID", decoded.Trim());

                                        com1.Parameters.AddWithValue("@time", txttime.Text);


                                        com1.ExecuteNonQuery();
                                    }
                                    Form2 form2 = new Form2();
                                    form2.Show();
                                    //MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    //MetroFramework.MetroMessageBox.Show(this, "this ID not Exist ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    Form3 form3 = new Form3();
                                    form3.Show();

                                }
                            }
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            if (con.State == ConnectionState.Open)
                                con.Close();
                        }
                       
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }





我认为可能是(Decode)的问题,会是什么正确的方法。



推荐答案

我们告诉了多少次你现在?永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



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

How many times have we told you now? 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. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

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

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;--'

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

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

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?


private void Timer1_Tick(object sender, EventArgs e)
       {
           BarcodeReader Reader = new BarcodeReader();
           Result result = Reader.Decode((Bitmap)pictureBox1.Image);
           try
           {
               //string decoded = result.ToString().Trim();
               if (result != null)
               {

                   //MessageBox.Show(result.Text);

                   using (SqlConnection con = new SqlConnection("Data Source=SQL5037.site4now.net;Initial Catalog=DB_A448D1_Dragon;User Id=************;Password=**********"))
                   {
                       con.Open();
                       try
                       {
                           using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                           {

                               com.Parameters.AddWithValue("@ID", result.Text);
                               com.Parameters.AddWithValue("@C1", DateTime.Now);
                               com.Parameters.AddWithValue("@C2", DateTime.Now);

                               int count = (int)com.ExecuteScalar();
                               if (count > 0)
                               {
                                   using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time) VALUES (@ID,@time)", con))
                                   {
                                       com1.Parameters.AddWithValue("@ID", result.Text);

                                       com1.Parameters.AddWithValue("@time", txttime.Text);


                                       com1.ExecuteNonQuery();
                                   }
                                   timer1.Stop();
                                   Form2 form2 = new Form2();
                                   form2.Show();
                                   this.Close();
                                   //MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                               }
                               else
                               {
                                   timer1.Stop();
                                   //MetroFramework.MetroMessageBox.Show(this, "this ID not Exist ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                   Form3 form3 = new Form3();
                                   form3.Show();
                                   this.Close();

                               }

                           }
                       }

                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.Message);
                       }
                       finally
                       {
                           if (con.State == ConnectionState.Open)
                               con.Close();
                       }

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


这篇关于使用qr代码C#winform的出勤申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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