我在使用vb.net在ms访问数据库中插入数据时遇到问题。
以下是我的代码,不知怎的,我收到错误"查询值的数量与字段"不同。是的,我尝试使用不同的代码并尽可能多地学习,但这是迄今为止我所获得的最佳代码。有人可以帮帮我吗?
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Try
If con.State = ConnectionState.Closed Then
con.ConnectionString = con_str
con.Open()
End If
oleDA.InsertCommand = New OleDb.OleDbCommand
oleDA.InsertCommand.Connection = con
oleDA.InsertCommand.CommandText = "INSERT INTO tblPlaylist VALUES (@Song,@Artist,@Album)"
oleDA.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("@Song", txtboxSong.Text))
oleDA.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("@Artist", txtboxArtist.Text))
oleDA.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("@Album", txtboxAlbum.Text))
oleDA.InsertCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error:" & ex.Message)
End Try
End Sub
答案 0 :(得分:1)
您需要定义要插入的列以及要插入其中的值。改变这一行:
oleDA.InsertCommand.CommandText = "INSERT INTO tblPlaylist VALUES (@Song,@Artist,@Album)"
到此
oleDA.InsertCommand.CommandText = "INSERT INTO tblPlaylist (Song, Artist, Album) VALUES (@Song,@Artist,@Album)"