以下是一个网友的问题,我也遇到了,记得在 更新前 Me.Contract_DesDA.Update(Me.ds.Tables("合同内容列表"))
要 Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(Contract_DesDA)
经测试,数据库定义了主键,在ds里面没必要像下文再定义了
~~~~~~~~~~~~~~~~~~
运行时,在datagridview中修改数据,然后点击Button1,预期结果:更新数据到Access,实际结果:提示"当传递具有已修改行的DataRow集合时,更新要求有效的UpdateCommand"
另外说明一下:1 使用的是Access 2003 2.tContract_Des这张表中已经设置主键了。
代码补齐如下:
======================================
Option Strict On
Imports System.Data.OleDb
Public Class Form1
Private ds As New DataSet ' 数据集对象的类别层级建立
Private cnnStringBuilder As New OleDbConnectionStringBuilder() ' 利用 OleDbConnectionStringBuilder 对象来构建连接字符串。
Private cn As OleDbConnection
Private Contract_DesDA As OleDbDataAdapter
Private cmContract_Des As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cnnStringBuilder.DataSource = "F:\外贸\test.mdb"
cnnStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0"
cmContract_Des = "SELECT * FROM tContract_Des"
cn = New OleDbConnection(cnnStringBuilder.ConnectionString)
Contract_DesDA = New OleDbDataAdapter(cmContract_Des, cn)
Contract_DesDA.Fill(ds, "合同内容列表")
Dim myPIColArray(1) As DataColumn
myPIColArray(0) = ds.Tables("合同内容列表").Columns("Contract_Des_ID")
ds.Tables("合同内容列表").PrimaryKey = myPIColArray
Me.BindingSource1.DataSource = ds.Tables("合同内容列表")
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(Contract_DesDA)
Me.Contract_DesDA.Update(Me.ds.Tables("合同内容列表"))
End Sub
End Class
http://topic.csdn.net/u/20071209/15/d9b0ddc9-de7b-40c7-ae58-7a7a584a28a2.html
======================================