Vb.net Visual studio:错误:参数?\u 1没有默认值

Vb.net Visual studio:错误:参数?\u 1没有默认值,vb.net,visual-studio,parameters,Vb.net,Visual Studio,Parameters,我正在处理的代码有问题,我到处搜索,似乎找不到任何可以帮助我的东西 公共类客户控制器 Public Const CONNECTION_STRING As String = _ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=assignment.accdb" Public Sub insert(ByVal htData As Hashtable) Dim oConnection As OleDbConnection = New Ol

我正在处理的代码有问题,我到处搜索,似乎找不到任何可以帮助我的东西


公共类客户控制器

Public Const CONNECTION_STRING As String = _
  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=assignment.accdb"

Public Sub insert(ByVal htData As Hashtable)

    Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING)

    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.VarChar, 255)
        oCommand.Parameters.Add("address", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("email", OleDbType.VarChar, 255)
        oCommand.Parameters.Add("dob", OleDbType.VarChar, 255)



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

        oCommand.Prepare()

        Debug.Print("SQL: " & oCommand.CommandText)

        oCommand.ExecuteNonQuery()

        Debug.Print("The record was inserted.")

        'If an error has occurred, this will show an error message to inform user that the record was not inserted
    Catch ex As Exception
        Debug.Print("ERROR: " & ex.Message)
        MsgBox("An error occurred. The record wasn't inserted.")
    Finally
        oConnection.Close()
    End Try

End Sub
末级


当我输入要插入数据库的数据时,我得到消息“错误:参数?\u 1没有默认值”
如果有人能帮助我,我将不胜感激!!谢谢

试试这个。如果这不起作用,则数据库中可能有一列需要值,但没有提供值,或者查询中可能缺少该列。此外,使用块自动处理对象(如连接和命令)也很好

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

Public Sub insert(htData As Hashtable)
    Try
        Using cn = New OleDbConnection(CONNECTION_STRING)
            cn.Open()
            Using cmd = New OleDbCommand("INSERT INTO Customer (title, gender, firstname, lastname, phone, address, email, dob) VALUES (?,?,?,?,?,?,?,?)", cn)
                With cmd.Parameters
                    .AddWithValue("title", htData("title"))
                    .AddWithValue("gender", htData("gender"))
                    .AddWithValue("firstname", htData("firstname"))
                    .AddWithValue("lastname", htData("lastname"))
                    .AddWithValue("phone", htData("phone"))
                    .AddWithValue("address", htData("address"))
                    .AddWithValue("email", htData("email"))
                    .AddWithValue("dob", htData("dob"))
                End With

                cmd.ExecuteNonQuery()
            End Using
        End Using

        Debug.Print("The record was inserted.")

        'If an error has occurred, this will show an error message to inform user that the record was not inserted
    Catch ex As Exception
        Debug.Print("ERROR: " & ex.Message)
        MsgBox("An error occurred. The record wasn't inserted.")
    End Try
End Sub
您可以尝试以下方法:

    oCommand.Parameters("title").Value = IIf(CStr(htData("title")) = "", DBNull.Value, CStr(htData("title")))
    oCommand.Parameters("gender").Value = IIf(CStr(htData("gender")) = "", DBNull.Value, CStr(htData("gender")))
    oCommand.Parameters("firstname").Value = IIf(CStr(htData("firstname")) = "", DBNull.Value, CStr(htData("firstname")))
    oCommand.Parameters("lastname").Value = IIf(CStr(htData("lastname")) = "", DBNull.Value, CStr(htData("lastname")))
    oCommand.Parameters("phone").Value = IIf(CStr(htData("phone")) = "", DBNull.Value, CStr(htData("phone")))
    oCommand.Parameters("address").Value = IIf(CStr(htData("address")) = "", DBNull.Value, CStr(htData("address")))
    oCommand.Parameters("email").Value = IIf(CStr(htData("email")) = "", DBNull.Value, CStr(htData("email")))
    oCommand.Parameters("dob").Value = IIf(CStr(htData("dob")) = "", DBNull.Value, CStr(htData("dob")))

使用调试器,检查htData(“title”)的值。看起来没什么。是的,这也不起作用,一定是和我的数据库有关:(感谢您的回复和帮助!!这可能是一个查询问题。您可能希望在cmd.ExecuteNonQuery上设置一个断点并检查命令文本。将其粘贴到查询编辑器中,查看是否返回错误。此时应该很容易发现。祝您好运!