0

私は自分のコードを自分でパラメーター化しようとしましたが、それを壊した可能性があります。これで、アプリケーションでレコードをデータベースに挿入できるようになりました。誰でもこのコードを見て、何が欠けているか教えてもらえますか?

編集: 提案されているように、コードを変更して dbCmd.Dispose() および dbConn.Close() メソッドを削除しました。現在、VB はデバッグ中に dbCmd.ExecuteNonQuery() 行で次の例外をスローしています。

列数が行 1 の値数と一致しません

ここに私のコードがあります:

    Private Sub addCard()
    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)
    Using dbConn As New MySqlConnection(ConnectionString)
        dbConn.Open()
        'PERFORM CARD ENCRYPTION
        Call encryptCard()

        'PERFORM DATABASE SUBMISSION
        Dim dbQuery As String = "INSERT INTO cc_master (ccType, cardholderFirstname, cardholderLastname, cardholderSalutation, ccLocation, " & _
                                 "ccNumber, ccExpireMonth, ccExpireYear, ccZipcode, ccCode, ccAuthorizedUseStart, ccAuthorizedUseEnd, " & _
                                 "dateAdded, addedBy, customer_accountNumber)" & _
                                 "VALUES(@ccType, @cardholderFirstname, @cardholderLastname, @cardholderSalutation, @ccLocation, " & _
                                 "@ccNumber, @ccExpireMonth, @ccExpireYear, @ccZipcode, @ccCode, @ccAuthorizedUseStart, @ccAuthorizedUseEnd " & _
                                 "@dateAdded, @addedBy, @accountNumber)"

        Using dbCmd As New MySqlCommand
            With dbCmd
                .Connection = dbConn
                .CommandType = CommandType.Text
                .CommandText = dbQuery
                .Parameters.AddWithValue("@ccType", ComboBoxCardType.Text)
                .Parameters.AddWithValue("@cardholderFirstname", TextBoxFirstName.Text)
                .Parameters.AddWithValue("@cardholderLastname", TextBoxLastName.Text)
                .Parameters.AddWithValue("@cardholderSalutation", ComboBoxSalutation.Text)
                .Parameters.AddWithValue("@ccLocation", TextBoxLocation.Text)
                .Parameters.AddWithValue("@ccNumber", encryptedCard)
                .Parameters.AddWithValue("@ccExpireMonth", TextBoxExpireMonth.Text)
                .Parameters.AddWithValue("@ccExpireYear", TextBoxExpireYear.Text)
                .Parameters.AddWithValue("@ccZipcode", TextBoxZipCode.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("@dateAdded", Format(DateTime.Now, "yyyy-MM-dd HH:MM:ss"))
                .Parameters.AddWithValue("@addedBy", FormLogin.TextBoxUsername.Text)
                .Parameters.AddWithValue("@accountNumber", TextBoxAccount.Text)
            End With
            Try
                Dim affectedRow As Integer
                affectedRow = dbCmd.ExecuteNonQuery()
                If affectedRow > 0 Then
                    MsgBox("Credit/Debit Card Information Saved SUCCESSFULLY!", MsgBoxStyle.Information, "RECORD SAVED")
                    ButtonReset.PerformClick()
                Else
                    MsgBox("Payment Card Was Not Added!", MsgBoxStyle.Critical, "ATTENTION")
                End If
            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
            dbCmd.Dispose()
        End Using
    End Using
    dbConn.Close()

End Sub

変更されたコード - 例外をスローするようになりました:

    Private Sub addCard()
    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)
    Using dbConn As New MySqlConnection(ConnectionString)
        'PERFORM CARD ENCRYPTION
        Call encryptCard()

        'PERFORM DATABASE SUBMISSION
        Dim dbQuery As String = "INSERT INTO cc_master (ccType, cardholderFirstname, cardholderLastname, cardholderSalutation, ccLocation, " & _
                                 "ccNumber, ccExpireMonth, ccExpireYear, ccZipcode, ccCode, ccAuthorizedUseStart, ccAuthorizedUseEnd, " & _
                                 "dateAdded, addedBy, customer_accountNumber)" & _
                                 "VALUES(@ccType, @cardholderFirstname, @cardholderLastname, @cardholderSalutation, @ccLocation, " & _
                                 "@ccNumber, @ccExpireMonth, @ccExpireYear, @ccZipcode, @ccCode, @ccAuthorizedUseStart, @ccAuthorizedUseEnd " & _
                                 "@dateAdded, @addedBy, @accountNumber)"

        Using dbCmd As New MySqlCommand
            With dbCmd
                .Connection = dbConn
                .CommandType = CommandType.Text
                .CommandText = dbQuery
                .Parameters.AddWithValue("@ccType", ComboBoxCardType.Text)
                .Parameters.AddWithValue("@cardholderFirstname", TextBoxFirstName.Text)
                .Parameters.AddWithValue("@cardholderLastname", TextBoxLastName.Text)
                .Parameters.AddWithValue("@cardholderSalutation", ComboBoxSalutation.Text)
                .Parameters.AddWithValue("@ccLocation", TextBoxLocation.Text)
                .Parameters.AddWithValue("@ccNumber", encryptedCard)
                .Parameters.AddWithValue("@ccExpireMonth", TextBoxExpireMonth.Text)
                .Parameters.AddWithValue("@ccExpireYear", TextBoxExpireYear.Text)
                .Parameters.AddWithValue("@ccZipcode", TextBoxZipCode.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("@dateAdded", Format(DateTime.Now, "yyyy-MM-dd HH:MM:ss"))
                .Parameters.AddWithValue("@addedBy", FormLogin.TextBoxUsername.Text)
                .Parameters.AddWithValue("@accountNumber", TextBoxAccount.Text)
            End With
            Try
                dbConn.Open()
                dbCmd.ExecuteNonQuery()
                Dim affectedRow As Integer
                affectedRow = dbCmd.ExecuteNonQuery()
                If affectedRow > 0 Then
                    MsgBox("Credit/Debit Card Information Saved SUCCESSFULLY!", MsgBoxStyle.Information, "RECORD SAVED")
                    ButtonReset.PerformClick()
                Else
                    MsgBox("Payment Card Was Not Added!", MsgBoxStyle.Critical, "ATTENTION")
                End If
            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
4

1 に答える 1

1

問題の解決策を見つけました。クエリの @ccAuthorizedUseEnd の末尾にカンマがありませんでした。私はそれとビオラを追加しました。エラーはなくなり、クエリは現在機能しています。

ありがとう。

于 2012-12-24T20:57:33.870 に答える