我正在使用下面的代碼來更新我的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
你可以檢查'ListViewCard.SelectedItems(0).ToString()'產生一個存在於'cc_master'表中的ccID嗎?我也會爲此創建一個參數。 – Laurence
爲什麼您對所有更新值使用查詢參數,而不是where條件? **你也需要參數化!** –
謝謝,但我確實證實了ccID確實存在。實際上,Listview中的數據是從數據庫中提取的。 –