我正在处理我正在处理的代码问题,我已经搜索过,似乎找不到任何可以帮助我的东西!
Public Class CustomerController
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
结束班
当我输入是将数据插入我的数据库时,我收到消息“错误:参数?_1没有默认值” 如果有人能帮助我,真的很感激!!感谢!!!
答案 0 :(得分:0)
试试这个。如果这不起作用,则数据库中可能存在一个需要值但未提供任何值的列,或者查询中可能缺少该列。此外,使用“使用块”自动处理诸如连接和命令之类的对象也是一件好事。
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
答案 1 :(得分:0)
你可以试试这个:
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")))