【问题标题】:error syntax in update statement更新语句中的错误语法
【发布时间】:2018-04-10 07:38:33
【问题描述】:

我想知道我更新用户时的语法有什么问题,当我更新时它总是会在更新语句中显示错误语法

 With cmd
                    .Connection = con
                    .CommandText = ("UPDATE [User] SET Username='" & TextBox2.Text & "',FirstName='" & TextBox3.Text & "', LastName='" & TextBox4.Text & "',Password='" & TextBox5.Text & "' Where ID = '" & TextBox1.Text & "' ")
                    .ExecuteNonQuery()
                    .Dispose()
                    TextBox1.Text = ""
                    TextBox2.Text = ""
                    TextBox3.Text = ""
                    TextBox4.Text = ""
                    TextBox5.Text = ""

                    MsgBox("User Updated", vbInformation, "Information Message")
                    datagridShow1()
                    con.Close()
end with

我也试过这个代码

 With cmd
                    .Connection = con
                    .CommandText = ("UPDATE [User] SET Username='" & TextBox2.Text & "',FirstName='" & TextBox3.Text & "', LastName='" & TextBox4.Text & "',Password='" & TextBox5.Text & "' where [ID]=@UID ")
                    .Parameters.AddWithValue("UID", CInt(TextBox1.Text))
                    .ExecuteNonQuery()
                    .Dispose()
                    TextBox1.Text = ""
                    TextBox2.Text = ""
                    TextBox3.Text = ""
                    TextBox4.Text = ""
                    TextBox5.Text = ""

                    MsgBox("User Updated", vbInformation, "Information Message")
                    datagridShow1()
                    con.Close()
 end with

【问题讨论】:

  • 尝试使用@ 传递参数,例如.Parameters.AddWithValue("@UID"
  • 问题不止一个。您是否尝试在 Microsoft Access 中使用 vb.net(而不是 vba)?您需要在问题中包含更多信息以及 MCVE。 (请参阅此链接了解它是什么:“minimal reproducible example”)
  • 我已经试过了,但还是有同样的问题
  • @ashleedawg 我正在使用 vb.net(Visual Studio Visual Basic)和我在 MS 访问中使用我的数据库。
  • 为什么.CommandText 在括号中?您是否尝试过在 Access 中运行它?也许您会收到更具体的错误消息。我看到你知道如何使用参数。现在您需要将所有其余部分都转换为参数。确保按照它们在查询中出现的顺序添加它们。这是 Access 的一个奇怪之处。

标签: vb.net ms-access


【解决方案1】:

Password 是 Access (JET/Ace) SQL 中的关键字。您需要将其括在括号中。另外,ID 是数字,所以不应该有单引号。

.CommandText = ("UPDATE [User] SET Username='" & TextBox2.Text & "',FirstName='" & TextBox3.Text & "', LastName='" & TextBox4.Text & "',[Password]='" & TextBox5.Text & "' Where ID = " & TextBox1.Text & " ")

请注意,据我所知,您的应用程序容易受到 SQL 注入攻击,并将密码存储为纯文本。这是两个最明显、最容易解决的安全问题。

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多