“INSERT INTO 语句中的语法错误”和“没有为一个或多个必需参数提供值”
Posted
技术标签:
【中文标题】“INSERT INTO 语句中的语法错误”和“没有为一个或多个必需参数提供值”【英文标题】:"Syntax error in INSERT INTO statement" and "No value given for one or more required parameters" 【发布时间】:2015-01-10 11:46:25 【问题描述】:所以我正在使用连接到 Access 2010 数据库的 Visual Basic 2010 进行我的第一个计算项目,并且我的项目中出现了两个不同的错误代码,我似乎无法理解:
在允许用户更新信息的按钮的代码中,我收到错误消息
没有为一个或多个必需参数指定值。
当我在调试时单击按钮时。
这是按钮的代码:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'returns items from text box to dataset
dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) = txtMemberID.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) = txtForename.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) = txtSurname.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) = txtDOB.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) = txtSex.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) = txtPhone.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) = txtEmail.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) = txtAddress.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) = txtPostcode.Text
daMember.Update(dsMember, "Members") 'updates items to database
MsgBox("Data updated")`
我收到错误消息的代码行是daMember.Update(dsMember, "Members")
行。我怎样才能让它工作,因为我觉得我已经尝试了一切。
在向数据库添加新记录的按钮的代码中,我收到一条错误消息
INSERT INTO 语句中的语法错误
当我在调试时单击按钮时,我又一次不知道如何修复它。单击按钮时,我收到错误消息。
这是按钮的代码:
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
If MemberRowNumber <> -1 Then 'checks for a valid record to add
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'declare new variables specifically for this statement
Dim dsMemberNewRow As DataRow
dsMemberNewRow = dsMember.Tables("Members").NewRow() 'adds new row
dsMemberNewRow.Item("MemberID") = txtMemberID.Text
dsMemberNewRow.Item("Forename") = txtForename.Text 'contains values to add to new row
dsMemberNewRow.Item("Surname") = txtSurname.Text
dsMemberNewRow.Item("DOB") = txtDOB.Text
dsMemberNewRow.Item("Sex") = txtSex.Text
dsMemberNewRow.Item("Phone_Number") = txtPhone.Text
dsMemberNewRow.Item("E-Mail_Address") = txtEmail.Text
dsMemberNewRow.Item("Home_Address") = txtAddress.Text
dsMemberNewRow.Item("Postcode") = txtPostcode.Text
dsMember.Tables("Members").Rows.Add(dsMemberNewRow) 'creates new object
daMember.Update(dsMember, "Members")
MsgBox("New Record added to the Database") 'display message once process is completed
End If
End Sub
与错误消息相关的代码行再次是daMember.Update(dsMember, "Members")
行。请帮忙,因为它让我沮丧地扯掉我的头发!
另外,由于这是我第一次在此网站上发布,因此如果措辞不当,我深表歉意,因此感谢您的耐心等待。提前谢谢!
这是完整的代码,如果有帮助的话:
Public Class member_details
Dim ParkwoodConnection As New OleDb.OleDbConnection 'this variable will now hold the control object
Dim Provider As String
Dim dbSource As String
Dim dsMember As New DataSet 'set up DataSet for members table
Dim daMember As OleDb.OleDbDataAdapter 'set up DataAdapter for members table
Dim dsRunning As New DataSet 'set up DataSet for Running table
Dim daRunning As OleDb.OleDbDataAdapter 'set up DataAdapter for Running table
Dim dsCycling As New DataSet 'set up DataSet for Cycling table
Dim daCycling As OleDb.OleDbDataAdapter 'set up DataAdapter for Cycling table
Dim dsSwimming As New DataSet 'set up DataSet for Swimming table
Dim daSwimming As OleDb.OleDbDataAdapter 'set up DataAdapter for Swimming table
Dim sqlMemberQuery As String
Dim sqlRunningQuery As String
Dim sqlCyclingQuery As String
Dim sqlSwimmingQuery As String
Dim MemberRowNumber As Integer
Dim MemberNumberOfRows As Integer
Dim dtRunning As New DataTable
Dim dtCycling As New DataTable
Dim dtSwimming As New DataTable
Private Sub AccessProject1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Provider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;" 'technology used to conntect to database
dbSource = "Data Source = training_log.accdb" 'pathname for database
ParkwoodConnection.ConnectionString = Provider & dbSource 'establish connection between form and database
ParkwoodConnection.Open() 'open database
sqlMemberQuery = "SELECT * FROM tblMembers" 'select all records from database
daMember = New OleDb.OleDbDataAdapter(sqlMemberQuery, ParkwoodConnection) 'pass object to daMember variable
daMember.Fill(dsMember, "Members") 'fills dataset with records from table
ParkwoodConnection.Close() 'close database
MemberNumberOfRows = dsMember.Tables("Members").Rows.Count 'get how many rows are in tblMembers
MemberRowNumber = 0 'set the intial row number to 0 when the form opens
DisplayData() 'display the data on to the form
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End Sub
Private Sub DisplayData()
txtMemberID.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) 'displays the members' ID in a text box
txtForename.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) 'displays the members' forename in a text box
txtSurname.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) 'displays the members' surname in a text box
txtDOB.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) 'displays the members' date of birth in a text box
txtSex.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) 'displays the members' sex
txtPhone.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) 'displays the members' phone number in a text box
txtEmail.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) 'displays the members' Emails in a text box
txtAddress.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) 'displays the members' address in a text box
txtPostcode.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) 'displays the members' postcode in a text box
End Sub
Private Sub btnNextRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnNextRecord.Click
If MemberRowNumber <> MemberNumberOfRows - 1 Then 'moves forward to next row
MemberRowNumber = MemberRowNumber + 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Else
MsgBox("No More Rows") 'display message when last row is reached
End If
End Sub
Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
If MemberRowNumber > 0 Then 'moves to previous row
MemberRowNumber = MemberRowNumber - 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Else
MsgBox("First Record") 'displays message if first row is reached
End If
End Sub
Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
If MemberRowNumber <> MemberNumberOfRows - 1 Then 'moves to last record
MemberRowNumber = MemberNumberOfRows - 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End If
End Sub
Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
If MemberRowNumber <> 0 Then 'moves to first record
MemberRowNumber = 0
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End If
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'returns items from text box to dataset
dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) = txtMemberID.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) = txtForename.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) = txtSurname.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) = txtDOB.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) = txtSex.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) = txtPhone.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) = txtEmail.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) = txtAddress.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) = txtPostcode.Text
daMember.Update(dsMember, "Members") 'updates items to database
MsgBox("Data updated")
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
txtMemberID.Clear()
txtForename.Clear() 'clears the forename text box
txtSurname.Clear() 'clears the surname text box
txtDOB.Clear() 'clears the Date of Birth text box
txtSex.Clear() 'clears the sex text box
txtPhone.Clear() 'clears the phone number text box
txtEmail.Clear() 'clears the email text box
txtAddress.Clear() 'clears the Address text box
txtPostcode.Clear() 'clears the postcode text box
dtRunning.Clear() 'clears the running results table
dtCycling.Clear() 'clears the cycling results table
dtSwimming.Clear() 'clears the swimming results table
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
MemberRowNumber = 0 'clears fields
DisplayData()
End Sub
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
If MemberRowNumber <> -1 Then 'checks for a valid record to add
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'declare new variables specifically for this statement
Dim dsMemberNewRow As DataRow
dsMemberNewRow = dsMember.Tables("Members").NewRow() 'adds new row
dsMemberNewRow.Item("MemberID") = txtMemberID.Text
dsMemberNewRow.Item("Forename") = txtForename.Text 'contains values to add to new row
dsMemberNewRow.Item("Surname") = txtSurname.Text
dsMemberNewRow.Item("DOB") = txtDOB.Text
dsMemberNewRow.Item("Sex") = txtSex.Text
dsMemberNewRow.Item("Phone_Number") = txtPhone.Text
dsMemberNewRow.Item("E-Mail_Address") = txtEmail.Text
dsMemberNewRow.Item("Home_Address") = txtAddress.Text
dsMemberNewRow.Item("Postcode") = txtPostcode.Text
dsMember.Tables("Members").Rows.Add(dsMemberNewRow) 'creates new object
daMember.Update(dsMember, "Members")
MsgBox("New Record added to the Database") 'display message once process is completed
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled") 'display message if record is not deleted
Exit Sub
End If
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember)
dsMember.Tables("Members").Rows(MemberRowNumber).Delete() 'selects row to delete
MemberNumberOfRows = MemberNumberOfRows - 1 'reduce number of rows
MemberRowNumber = 0 'display first record
DisplayData()
daMember.Update(dsMember, "Members") 'updates database
End Sub
End Class
【问题讨论】:
这表示您在数据库中的表中的列比您在代码中设置的列多,或者您在代码中拥有的列之一为 null 并且您已使其不为 null桌子。从这里看不到,但这是您应该寻找的。span> 尝试相关问题here中的建议,看看是否有帮助。 【参考方案1】:我多次遇到这个问题。 SQL查询的问题。这就是消息显示的原因是因为 SQL 解析器无法识别 SQL 查询中的标记并将其解释为您必须或需要提供值的参数。
给出价值。计算数据库的列,然后计算你有多少值。然后,给它一个参数,并确保你没有错过任何一个。
【讨论】:
以上是关于“INSERT INTO 语句中的语法错误”和“没有为一个或多个必需参数提供值”的主要内容,如果未能解决你的问题,请参考以下文章
“INSERT INTO 语句中的语法错误”和“没有为一个或多个必需参数提供值”
使用 DateTime 时 INSERT INTO 语句中的语法错误
将文本框值添加到单个记录:INSERT INTO 语句中的语法错误