数据集没有被更新vb.net

时间:2012-11-27 08:28:58

标签: vb.net binding dataset

在加载表单时,我使用以下代码填充数据集:

Con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Congress.accdb;Persist Security Info=False"
Con.Open()

dAp1 = New OleDbDataAdapter("Select * from VotingRecords", Con)
DS.Tables.Add("VotingRecords")
dAp1.Fill(DS.Tables("VotingRecords"))

BindingNavigator1.BindingSource = myBS
myBS.DataSource = DS.Tables("VotingRecords")

Me.BillComboBox.DataBindings.Add(New Binding("Text", myBS, "BillNumber", True))
Me.CongressPersonComboBox.DataBindings.Add(New Binding("Text", myBS, "CID", True))
Me.VoteComboBox.DataBindings.Add(New Binding("Text", myBS, "Vote", True))

此处所有Con,dAP1,DS,myBS变量都是全局变量或表单级变量。

单击保存按钮时:

Try
    myBS.EndEdit()
    dAp1.Update(DS.Tables("VotingRecords"))
Catch ex As Exception
    MsgBox("Error")
End Try

不确定为什么它会显示我的错误;错误讯息:

  

更新在传递DataRow集合时需要有效的UpdateCommand   修改过的行。

2 个答案:

答案 0 :(得分:0)

您需要在UpdateCommand上设置OleDbDataAdapter属性。

dAp1 = New OleDbDataAdapter("Select * from VotingRecords", Con)
dAp1.UpdateCommand = new OleDbCommand("UPDATE VotingRecords SET Column1 = ?, Column2 = ? WHERE Column3 = ?")
dAp1.UpdateCommand.Parameters.Add("Column1", column1OleDbType, column1Length, "Column1")
dAp1.UpdateCommand.Parameters.Add("Column2", column2OleDbType, column2Length, "Column2")
dAp1.UpdateCommand.Parameters.Add("Column3", column3OleDbType, column3Length, "Column3")
dAp1.UpdateCommand.Parameters("Column3").SourceVerion = DataRowVersion.Original

文档:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.updatecommand.aspx

答案 1 :(得分:0)

让它工作....感谢你 http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

我失踪的命令构建者