【发布时间】: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 的一个奇怪之处。