请帮我处理我的代码(代码扫描条形码) [英] Pls help me in my code (code scan bar code)

查看:78
本文介绍了请帮我处理我的代码(代码扫描条形码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The code below is 100% correct and fetches data from the database. It also fetches data from the form and then places it in List View when copying the code into a textbox. However, when using the bar code reader, the user code does not appear and the username does not appear on the list. Is the problem in the event of Text changed or what?





我尝试了什么:





What I have tried:

<pre>   If Len(Trim(CmbWTime.Text)) = 0 Then
            MessageBox.Show("من فضلك حدد فترة الدوام", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            CmbWTime.Focus()
            Exit Sub
        End If
        con = New SqlConnection(cs)
        con.Open()
        Dim strQ As String = String.Empty
        Dim cmd0 As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        strQ = " Select EmpCode,EmpName from Employees where EmpCode='" + TxtCode.Text + "'"
        cmd0 = New SqlCommand(strQ, con)
        da = New SqlDataAdapter(cmd0)
        ds = New DataSet
        da.Fill(ds, "Employees")
        If ds.Tables(0).Rows.Count > 0 Then
            txtEmpCode.Text = ds.Tables(0).Rows(0)("EmpCode").ToString()
            TxtEmpName.Text = ds.Tables(0).Rows(0)("EmpName").ToString()
        End If

        If TeMAL.Text = "لم يبدأ الدوام" Then
            Exit Sub
        ElseIf TeMAL.Text = "حضـر الموظف" Then
            'كود الإضافة إلى الليست فيو
            Dim temp As Integer
            temp = ListView1.Items.Count()
            If temp = 0 Then
                Dim i As Integer
                Dim lst As New ListViewItem(i)
                lst.SubItems.Add(txtEmpCode.Text) 'كود الموظف
                lst.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
                lst.SubItems.Add(Label9.Text) 'وقت الحضور
                lst.SubItems.Add("بالدوام") 'وقت الإنصراف
                lst.SubItems.Add(TeMAL.Text) 'حالة الموظف
                lst.SubItems.Add(Label5.Text) ' تاريخ الحضور
                lst.SubItems.Add(Label8.Text) ' يوم الحضور
                lst.SubItems.Add(CmbWTime.Text) ' فترة الحضور
                lst.SubItems.Add(RichTextBox1.Text) ' ملاحظات
                lst.SubItems.Add("حضر بالميعاد") ' حالة الغياب
                lst.SubItems.Add(LateTime.Text) ' معدل التأخير
                ListView1.Items.Add(lst)
                i = i + 1
                TxtCode.SelectAll()
                txtEmpCode.Text = ""
                TxtEmpName.Text = ""
                Exit Sub
            End If
            For j = 0 To temp - 1
                If (ListView1.Items(j).SubItems(1).Text = txtEmpCode.Text) And (ListView1.Items(j).SubItems(6).Text = Label5.Text) Then
                    If MessageBox.Show("هذا الموظف لايمكن تسجيلة مرتين", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
                        TxtCode.Clear()
                        TxtCode.Focus()
                    End If
                    Exit Sub
                End If
            Next j
            Dim k As Integer
            Dim lst1 As New ListViewItem(k)
            lst1.SubItems.Add(txtEmpCode.Text) 'كود الموظف
            lst1.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
            lst1.SubItems.Add(Label9.Text) 'وقت الحضور
            lst1.SubItems.Add("بالدوام") 'وقت الإنصراف
            lst1.SubItems.Add(TeMAL.Text) 'حالة الموظف
            lst1.SubItems.Add(Label5.Text) ' تاريخ الحضور
            lst1.SubItems.Add(Label8.Text) ' يوم الحضور
            lst1.SubItems.Add(CmbWTime.Text) ' فترة الحضور
            lst1.SubItems.Add(RichTextBox1.Text) ' ملاحظات
            lst1.SubItems.Add("حضر بالميعاد") ' حالة الغياب
            lst1.SubItems.Add(LateTime.Text) ' معدل التأخير
            ListView1.Items.Add(lst1)
            k = k + 1
            TxtCode.SelectAll()
            txtEmpCode.Text = ""
            TxtEmpName.Text = ""

        ElseIf TeMAL.Text = "تأخر الموظف" Then
            'كود الإضافة إلى الليست فيو
            Dim temp As Integer
            temp = ListView1.Items.Count()
            If temp = 0 Then
                Dim i As Integer
                Dim lst As New ListViewItem(i)
                lst.SubItems.Add(txtEmpCode.Text) 'كود الموظف
                lst.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
                lst.SubItems.Add(Label9.Text) 'وقت الحضور
                lst.SubItems.Add("بالدوام") 'وقت الإنصراف
                lst.SubItems.Add(TeMAL.Text) 'حالة الموظف
                lst.SubItems.Add(Label5.Text) ' تاريخ الحضور
                lst.SubItems.Add(Label8.Text) ' يوم الحضور
                lst.SubItems.Add(CmbWTime.Text) ' فترة الحضور
                lst.SubItems.Add(RichTextBox1.Text) ' ملاحظات
                lst.SubItems.Add("حضر متأخرا") ' حالة الغياب
                lst.SubItems.Add(LateTime.Text) ' معدل التأخير
                ListView1.Items.Add(lst)
                i = i + 1
                TxtCode.SelectAll()
                txtEmpCode.Text = ""
                TxtEmpName.Text = ""
                Exit Sub
            End If
            For j = 0 To temp - 1
                If (ListView1.Items(j).SubItems(1).Text = txtEmpCode.Text) And (ListView1.Items(j).SubItems(6).Text = Label5.Text) Then
                    MessageBox.Show("هذا الموظف لايمكن تسجيلة مرتين", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    TxtCode.SelectAll()
                    Exit Sub
                End If
            Next j
            Dim k As Integer
            Dim lst1 As New ListViewItem(k)
            lst1.SubItems.Add(txtEmpCode.Text) 'كود الموظف
            lst1.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
            lst1.SubItems.Add(Label9.Text) 'وقت الحضور
            lst1.SubItems.Add("بالدوام") 'وقت الإنصراف
            lst1.SubItems.Add(TeMAL.Text) 'حالة الموظف
            lst1.SubItems.Add(Label5.Text) ' تاريخ الحضور
            lst1.SubItems.Add(Label8.Text) ' يوم الحضور
            lst1.SubItems.Add(CmbWTime.Text) ' فترة الحضور
            lst1.SubItems.Add(RichTextBox1.Text) ' ملاحظات
            lst1.SubItems.Add("حضر متأخرا") ' حالة الغياب
            lst1.SubItems.Add(LateTime.Text) ' معدل التأخير
            ListView1.Items.Add(lst1)
            k = k + 1
            TxtCode.SelectAll()
            txtEmpCode.Text = ""
            TxtEmpName.Text = ""

        ElseIf TeMAL.Text = "إنتهى الدوام" Then
            Exit Sub
        End If
        TxtCode.Clear()
        TxtCode.Focus()

推荐答案

引用:

以下代码100%正确并从数据库中提取数据。

The code below is 100% correct and fetches data from the database.



不,不是。


No it is not.

strQ = " Select EmpCode,EmpName from Employees where EmpCode='" + TxtCode.Text + "'"



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

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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

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

PHP:SQL注入 - 手册 [ ^ ]

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


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[^]


该代码远不及100%正确。这很危险,每次用户运行它都会给数据库带来风险。



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



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

That code is nowhere near "100% correct". It's dangerous, and risks your database every time a user runs it.

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'

就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

--'

其他一切都是评论。

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



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



当你完成了整个应用程序并修复了所有这些内容时,请使用调试器来查看具体内容正在运行你的应用程序。由于问题取决于我们无法访问的数据,我们无法为您做到这一点 - 因此,这取决于您。

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



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


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

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?

When you have gone through your entire app and fixed all occurrences of that, use the debugger to look at exactly what is going on while you run your app. Since the problem depends on data that we do not have any access to, we can't do that for you - 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!


这篇关于请帮我处理我的代码(代码扫描条形码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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