在update语句中获取语法错误。 Vb.net [英] Getting syntax error in update statement. Vb.net

查看:118
本文介绍了在update语句中获取语法错误。 Vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击更新按钮时,我在更新语句中收到以下错误语法错误。 vb.net



我尝试过:



When i click on Update Button i am getting the following error syntax error in update statement. vb.net

What I have tried:

If con.State = ConnectionState.Open Then
            con.Close()
        End If
        If txtID.Text = "" Then
            MsgBox("Please Select Element To Update....", MsgBoxStyle.Information)
            Exit Sub
        End If
        ''UPDATE Login SET User = 'Nirmal', Pass= 'n@12345', Rights= 'Admin' WHERE id = 2

        Try
            Dim cmditem1 As New OleDbCommand("UPDATE LOGIN set USER = ('" & txtUser.Text & "'), PASS = ('" & txtPass.Text & "'), RIGHTS = ('" & cmbRights.Text & "') where ID = " & txtID.Text, con)
            con.Open()
            cmditem1.ExecuteNonQuery()
            MsgBox("Updated Successfully", MsgBoxStyle.Information)
            con.Close()

            clean()
            setg1()
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

<connectionstrings>
    <add name="cn" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\CONFIDENTIAL\YandexDisk\KAPYHA90\PAL\PAL_BillingDB.mdb;Jet OLEDB:Database Password=Pal@30384731;Jet OLEDB:Engine Type=5" providername="System.Data.OleDb">

推荐答案

因为您通过以下方式构建查询连接,我们无法知道什么是真正的查询,因为它依赖于输入字段。

Because you build the query by concatenation, we have no way to know what is the real query because it depend on input fields.
Quote:

Dim cmditem1 As New OleDbCommand("UPDATE LOGIN set USER = ('" & txtUser.Text & "'), PASS = ('" & txtPass.Text & "'), RIGHTS = ('" & cmbRights.Text & "') where ID = " & txtID.Text, con)





没有必要解决你的问题,但你有另一个问题。

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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

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

PHP:SQL注入 - 手册 [ ^ ]

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

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]



Not necessary 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[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


"UPDATE LOGIN set USER = ('" & txtUser.Text & "'), PASS = ('" & txtPass.Text & "'), RIGHTS = ('" & cmbRights.Text & "') where ID = " & txtID.Text



为什么要在括号)之间包含提供的值? SQL语法错误。

真正的语法是


Why are you enclosing provided values between brackets ( and )? The SQL syntax is wrong.
The real syntax is

"UPDATE LOGIN set USER = 'username', PASS = 'pass', RIGHTS = 'rights' where ID = id



但是:

正如在解决方案1中告诉你的那样,从不通过连接从用户输入获得的值来构建SQL字符串。这是你应该先解决的问题。

Plus,您不应该以明文形式存储密码。


BUT:
As told you in solution 1, never build SQL strings by concatenating values obtained from user input. This is the issue that you should fix first.
Plus, you should not store passwords in clear text, either.


修复SQL注入漏洞很容易:

Fixing the SQL Injection vulnerability is the easy part:
Dim cmditem1 As New OleDbCommand("UPDATE LOGIN set USER = ?, PASS = ?, RIGHTS = ? WHERE ID = ?", con)

' For OLEDB, parameter names don't matter; 
' they just need to be added in the same order as they appear in the query.

cmditem1.Parameters.AddWithValue("user", txtUser.Text)
cmditem1.Parameters.AddWithValue("pass", txtPass.Text)
cmditem1.Parameters.AddWithValue("rights", cmbRights.Text)
cmditem1.Parameters.AddWithValue("ID", txtID.Text)

con.Open()
cmditem1.ExecuteNonQuery()

现在你需要完成其余的代码并修复任何其他没有正确使用参数的查询。





但是,您还需要修复密码存储空间。您目前正在以纯文本格式存储密码,这是一个非常糟糕的主意,并且可能会让您受到巨额罚款。相反,您需要存储密码的盐渍哈希,为每条记录使用唯一的盐,并使用多轮密码派生函数,如 PBKDF2 [ ^ ]。



简单解释安全密码验证 [ ^ ]

Salted Password Hashing - 正确行事 [ ^ ]

Now you'll need to go through the rest of your code and fix any other queries which aren't using parameters correctly.


However, you also need to fix your password storage. You're currently storing passwords in plain text, which is an extremely bad idea, and could leave you open to massive fines. Instead, you need to store a salted hash of the password, using a unique salt for each record, and using multiple rounds of a password derivation function like PBKDF2[^].

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]


这篇关于在update语句中获取语法错误。 Vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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