gpt4 book ai didi

mysql - 更新记录时 AddWithValue 不起作用

转载 作者:行者123 更新时间:2023-11-28 23:33:48 27 4
gpt4 key购买 nike

我有这个代码

    MySQL_Query = "UPDATE `owner_info` " _
& "SET first_name=" & first_name _
& ", last_name= " & last_name _
& ", middle_name=" & middle_name _
& ", address= " & address _
& ", contact_num= " & contact_num _
& " WHERE id=" & ownerID

Console.WriteLine(MySQL_Query)

Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn)
With frmOwner_Edit
If String.IsNullOrEmpty(first_name) = True Then first_name = .lblExisting_FirstName.Text
If String.IsNullOrEmpty(last_name) = True Then last_name = .lblExisting_LastName.Text
If String.IsNullOrEmpty(middle_name) = True Then middle_name = .lblExisting_MiddleName.Text
If String.IsNullOrEmpty(address) = True Then address = .lblExisting_Address.Text
If String.IsNullOrEmpty(contact_num) = True Then contact_num = .lblExisting_ContactNum.Text
End With
Try
MySQL_CMD.Connection.Open()
'MySQL_CMD.CommandText = MySQL_Query
MySQL_CMD.ExecuteNonQuery()
Catch myerror As MySqlException
Console.WriteLine("Error: Owner Info Insert")
Console.WriteLine(myerror)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try

而且它完美运行,我正在尝试应用 AddWithValue 但它不起作用?我觉得我在这里遗漏了一些东西。这是代码。

    MySQL_Query = "UPDATE `owner_info` " _
& "SET first_name='@first_name', last_name='@last_name', middle_name='@middle_name', address='@address', contact_num='@contact_num' " _
& "WHERE id='@id'"
Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn)
With frmOwner_Edit
If String.IsNullOrEmpty(first_name) = True Then first_name = .lblExisting_FirstName.Text
If String.IsNullOrEmpty(last_name) = True Then last_name = .lblExisting_LastName.Text
If String.IsNullOrEmpty(middle_name) = True Then middle_name = .lblExisting_MiddleName.Text
If String.IsNullOrEmpty(address) = True Then address = .lblExisting_Address.Text
If String.IsNullOrEmpty(contact_num) = True Then contact_num = .lblExisting_ContactNum.Text
End With
Try
MySQL_CMD.Connection.Open()
MySQL_CMD.CommandText = MySQL_Query
MySQL_CMD.Parameters.AddWithValue("@first_name", first_name)
MySQL_CMD.Parameters.AddWithValue("@last_name", last_name)
MySQL_CMD.Parameters.AddWithValue("@middle_name", middle_name)
MySQL_CMD.Parameters.AddWithValue("@address", address)
MySQL_CMD.Parameters.AddWithValue("@contact_num", contact_num)
MySQL_CMD.Parameters.AddWithValue("@id", ownerID)
MySQL_CMD.ExecuteNonQuery()
Catch myerror As MySqlException
Console.WriteLine("Error: Owner Info Insert")
Console.WriteLine(myerror)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try

最佳答案

移除参数周围的勾号。当它们存在时,MySql 将它们视为字符串而不是参数。因此错误可能是因为没有提供它需要的参数。

关于mysql - 更新记录时 AddWithValue 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com