1

Access データベースにレコードを作成するように設計されたこのコードでエラーが発生しましたが、その理由がわかりません。

Option Explicit On
Option Strict On

Imports System.Data.OleDb

'Name:          CustomerController.vb
'Description:   Class acting as intermediary between the Customer Form and Customer table
'               Contains Most of the CRUD business Logic
'Author:        Alastair McIntyre
'Date:          12/04/2015

Public Class CustomerController
    Public Const CONNECTION_STRING As String = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=assignment 1.accdb"

    Public Function insertCustomer(ByVal htCustomer As Hashtable) As Integer

        Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
        Dim iNumRows As Integer
        Try
            Debug.Print("Connection string: " & oConnection.ConnectionString)

            oConnection.Open()
            Dim oCommand As OleDbCommand = New OleDbCommand
            oCommand.Connection = oConnection

            oCommand.CommandText = _
                "INSERT INTO customer (title, gender, firstname, lastname, phone, address, email, dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"

            oCommand.Parameters.Add("title", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("gender", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("firstname", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("lastname", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("phone", OleDbType.Integer, 11)
            oCommand.Parameters.Add("address", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("email", OleDbType.VarChar, 255)
            oCommand.Parameters.Add("dob", OleDbType.Integer, 8)

            oCommand.Parameters("title").Value = CStr(htCustomer("title"))
            oCommand.Parameters("gender").Value = CStr(htCustomer("gender"))
            oCommand.Parameters("firstname").Value = CStr(htCustomer("firstname"))
            oCommand.Parameters("lastname").Value = CStr(htCustomer("lastname"))
            oCommand.Parameters("phone").Value = CInt(htCustomer("phone"))
            oCommand.Parameters("address").Value = CStr(htCustomer("address"))
            oCommand.Parameters("email").Value = CStr(htCustomer("email"))

            oCommand.Prepare()

            iNumRows = oCommand.ExecuteNonQuery()
            Debug.Print(CStr(iNumRows))

            Debug.Print("The record was insterted")
            'Catch ex As Exception
            'Debug.Print("Error: " & ex.Message)
            'MsgBox("An error occured. The record wasn't inserted")
        Finally
            oConnection.Close()
        End Try

        Return iNumRows
    End Function
End Class

エラーメッセージをコメントアウトしてエラーをデバッグした後、「http://i.stack.imgur.com/fQgBJ.png」でエラーが発生していることがわかりました。これがDebubモードで発生すると、アプリケーションがクラッシュし、リンクされたデータベースにレコードを作成しません

4

1 に答える 1

1

エラー メッセージを読みます。

パラメータ ?_8 にはデフォルト値がありません。

クエリには 8 つのパラメーター プレースホルダーがあります。

oCommand.CommandText = _
            "INSERT INTO customer (title, gender, firstname, lastname, phone, address, email, dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"

次に、8 つのパラメーターを追加します。

oCommand.Parameters.Add("title", OleDbType.VarChar, 255)
oCommand.Parameters.Add("gender", OleDbType.VarChar, 255) 
oCommand.Parameters.Add("firstname", OleDbType.VarChar, 255)
oCommand.Parameters.Add("lastname", OleDbType.VarChar, 255)
oCommand.Parameters.Add("phone", OleDbType.Integer, 11)
oCommand.Parameters.Add("address", OleDbType.VarChar, 255)
oCommand.Parameters.Add("email", OleDbType.VarChar, 255)
oCommand.Parameters.Add("dob", OleDbType.Integer, 8)

次に... 7 つの値を設定します。

oCommand.Parameters("title").Value = CStr(htCustomer("title"))
oCommand.Parameters("gender").Value = CStr(htCustomer("gender"))
oCommand.Parameters("firstname").Value = CStr(htCustomer("firstname"))
oCommand.Parameters("lastname").Value = CStr(htCustomer("lastname"))
oCommand.Parameters("phone").Value = CInt(htCustomer("phone"))
oCommand.Parameters("address").Value = CStr(htCustomer("address"))
oCommand.Parameters("email").Value = CStr(htCustomer("email"))

8 番目のパラメーター ("dob") の値を設定する必要があります。

(補足: Phone と DOB は整数値ではないため、おそらく整数であってはなりません。Phone はテキスト値であり、DOB は日付値です。)

于 2015-04-22T23:27:34.420 に答える