晚上好,
我正在使用以下代码更新MySQL数据库中的值。在运行时,代码一直执行而没有错误,但是,当我查看数据库时,记录没有受到影响。有人能告诉我我做错了吗?感谢。
我最近自学了如何使用参数,所以也许我可能做错了 - 不知道。
这是我的代码:
Private Sub updateCard()
Call encryptCard()
Dim ConnectionString As String = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
Dim myQuery As String = "UPDATE cc_master " & _
"SET ccType = @ccType, ccNumber = @ccNumber, ccExpireMonth = @ccExpireMonth, " & _
"ccExpireYear = @ccExpireYear, ccCode = @ccCode, ccAuthorizedUseStart = @ccAuthorizedUseStart, " & _
"ccAuthorizedUseEnd = @ccAuthorizedUseEnd, ccZipcode = @ccZipcode, dateModified = @dateModified, modifiedBy = @modifiedBy " & _
"WHERE ccID = @ccID"
Using dbConn As New MySqlConnection(ConnectionString)
Using dbComm As New MySqlCommand()
With dbComm
.Connection = dbConn
.CommandType = CommandType.Text
.CommandText = myQuery
.Parameters.AddWithValue("@ccID", ListViewCard.SelectedItems(0).ToString)
.Parameters.AddWithValue("@ccType", ComboBoxCardType.Text)
.Parameters.AddWithValue("@ccNumber", encryptedCard)
.Parameters.AddWithValue("@ccExpireMonth", TextBoxExpireMonth.Text)
.Parameters.AddWithValue("@ccExpireYear", TextBoxExpireYear.Text)
.Parameters.AddWithValue("@ccCode", TextBoxCVV2.Text)
.Parameters.AddWithValue("@ccAuthorizedUseStart", Format(DateTimePickerStartDate.Value, "yyyy-MM-dd HH:MM:ss"))
.Parameters.AddWithValue("@ccAuthorizedUseEnd", Format(DateTimePickerEndDate.Value, "yyyy-MM-dd HH:MM:ss"))
.Parameters.AddWithValue("@ccZipcode", TextBoxZipCode.Text)
.Parameters.AddWithValue("@dateModified", Format(DateTime.Now, "yyyy-MM-dd HH:MM:ss"))
.Parameters.AddWithValue("@modifiedBy", FormLogin.TextBoxUsername.Text)
End With
Try
dbConn.Open()
dbComm.ExecuteNonQuery()
MessageBox.Show("Card info SUCCESSFULLY updated!")
Catch ex As Exception
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
End Try
End Using
End Using
End Sub
答案 0 :(得分:3)
尝试将dbComm.ExecuteNonQuery()
更改为:
Try
Dim affectedRow As Integer
dbConn.Open()
affectedRow = dbComm.ExecuteNonQuery()
if affectedRow > 0 then
MessageBox.Show("Card info SUCCESSFULLY updated!")
else
MessageBox.Show("No data to be updated!")
End If
Catch ex As Exception
....
....
End Try
这将告诉你应该更新多少行。
答案 1 :(得分:0)
早上好,
经过几个小时的调试和Brandon在www.bpforums.com的一些帮助后,找到了以下解决方案。查询现在正确执行,DB也会受到相应的影响。
解决方案:
Bradon @ bpforums.com向我指出,我的数据库中的ccID的数据类型是INT - 因此,我需要将listview选择的值转换为整数。然后我意识到我需要调用 .text 而不是 .ToString 来获取索引的实际文本值,然后才能转换为整数。
.Parameters.AddWithValue("@ccID", CInt(ListViewCard.SelectedItems(0).Text))
感谢@habibillah,@ Laurence和@Adrian Cornish。你指出了我正确的方向。