ADO.NET错误:参数?_1没有默认值

时间:2016-04-08 15:20:45

标签: .net vb.net ado.net

我按照我在大学学习的方式编写代码,但它显示错误

  

参数?_1没有默认值。

我在互联网上阅读了不同的解决方案,但它们都不适用于我的问题。

以下是相关代码:

Public Class StudentController
    Public Const CONNECTION_STRING As String =
    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Tutoringsystem.accdb"

    Public Function insert(ByVal htdata 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 students (first_name, last_name, street_address, suburb, state, postcode, telephone, email, dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"

        oCommand.Parameters.Add("FirstName", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("LastName", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("StreetAddress", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("Suburb", OleDbType.VarChar, 4)
        oCommand.Parameters.Add("State", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("Postcode", OleDbType.VarChar, 4)
        oCommand.Parameters.Add("Telephone", OleDbType.VarChar, 12)
        oCommand.Parameters.Add("Email", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("DOB", OleDbType.VarChar, 8)

        oCommand.Parameters("FirstName").Value = CStr(htdata("first_name"))
        oCommand.Parameters("LastName").Value = CStr(htdata("last_name"))
        oCommand.Parameters("StreetAddress").Value = CStr(htdata("street_address"))
        oCommand.Parameters("Suburb").Value = CInt(htdata("suburb"))
        oCommand.Parameters("State").Value = CStr(htdata("state"))
        oCommand.Parameters("Postcode").Value = CStr(htdata("postcode"))
        oCommand.Parameters("Telephone").Value = CStr(htdata("telephone"))
        oCommand.Parameters("Email").Value = CStr(htdata("email"))
        oCommand.Parameters("DOB").Value = CStr(htdata("dob"))

        oCommand.Prepare()

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

        Debug.Print("The record was inserted")

    Catch ex As Exception
        Debug.Print("ERROR:    " & ex.Message)
        MsgBox("An error occurred. The record wasn't inserted.")
    Finally
        oConnection.Close()

    End Try

    Return iNumRows

End Function

0 个答案:

没有答案