我正在尝试从VB 2015代码更新MS Access数据库。数据集信息未返回数据库。
我已经阅读了这里的答案,说您在更新之前不能接受AcceptChanges,但是如果注释掉了,那么下面的da.Update(ds)给出:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional Information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
数据库TestDb –一个表= TestTable –设计:
Name Type Size
-------- ------- ------
ID Long Integer 4 Primary Key
Name Text 255
NumberOne Long Integer 4
NumberTwo Long Integer 4
内容:
测试表
ID Name NumberOne NumberTwo
1 EntryOne 1
2 EntryTwo 2
3 EntryThree 3
4 EntryFour 4
5 EntryFive 5
请注意,“ NumberTwo”列有意留为空白。
程式DbTest.vb
在启动时,程序将数据库加载到数据集中。<<,<,>和>>按钮可用于单步执行数据。
“执行”按钮用“ NumberOne”列中条目的平方填充数据集的“ NumberTwo”列。
“保存”按钮尝试通过数据适配器将修改后的数据集保存回数据库。然后将数据库加载到第二个数据集中,以检查第一个数据集是否已正确保存回数据库中。通过逐步浏览数据,可以发现保存失败。
我的代码:
'**********
' DbTest.vb
' Version 0.00
' MDJ 2015/11/11
'**********
Imports System
Imports System.IO
Imports System.Text
Public Class Form1
Dim conn As NewOleDb.OleDbConnection(connectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TestDB.accdb;Persist Security Info=False;")
Dim strSQL As String = "SELECT * FROM TestTable"
Dim da As New OleDb.OleDbDataAdapter(strSQL, conn)
Dim ds As New DataSet()
Dim ds2 As New DataSet()
Dim intCurrentIndex As Integer
Dim dSaved As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dSaved = False
txtError.Text = "No Exceptions"
Try
conn.Open()
Catch ex As OleDb.OleDbException
txtError.Text = "OleDbException"
GoTo errExit
Catch ex As DataException
txtError.Text = "DataException"
GoTo errExit
Catch ex As Exception
txtError.Text = "Other Exception"
GoTo errExit
End Try
txtError.Text = "Error: ds Is Empty"
' Fill dataset from database
da.Fill(ds)
'Check if the Table is empty
If ds.Tables(0).Rows.Count > 0 Then
txtError.Text = "No Error"
End If
errExit:
conn.Close()
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
'Since 0 is the first row
intCurrentIndex = 0
txtID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("ID").ToString()
txtName.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Name").ToString()
txtNumberOne.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberOne").ToString()
txtNumberTwo.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
If dSaved = True Then
txtDaNumberTwo.Text = ds2.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
'We move back only if we're not at the first row.
If intCurrentIndex > 0 Then
'Subtract one from the current index.
intCurrentIndex = intCurrentIndex - 1
txtID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("ID").ToString()
txtName.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Name").ToString()
txtNumberOne.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberOne").ToString()
txtNumberTwo.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
If dSaved = True Then
txtDaNumberTwo.Text = ds2.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
End If
Else
MessageBox.Show("You're already at the first record.")
End If
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'ds.Tables(0).Rows.Count - 1 is the index for the last row
'We move forward only if we're not at the last row.
If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then
'Add one to the current index.
intCurrentIndex = intCurrentIndex + 1
txtID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("ID").ToString()
txtName.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Name").ToString()
txtNumberOne.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberOne").ToString()
txtNumberTwo.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
If dSaved = True Then
txtDaNumberTwo.Text = ds2.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
End If
Else
MessageBox.Show("You're already at the last record.")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
'ds.Tables(0).Rows.Count - 1 is the index for the last row
intCurrentIndex = ds.Tables(0).Rows.Count - 1
txtID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("ID").ToString()
txtName.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Name").ToString()
txtNumberOne.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberOne").ToString()
txtNumberTwo.Text = ds.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
If dSaved = True Then
txtDaNumberTwo.Text = ds2.Tables(0).Rows(intCurrentIndex).Item("NumberTwo").ToString()
End If
End Sub
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim n1 As Integer
Dim n2 As Integer
'ds.Tables(0).Rows.Count - 1 is the index for the last row
Dim intLastCount As Integer
intLastCount = ds.Tables(0).Rows.Count - 1
' Process each record
For intCurrentIndex = 0 To intLastCount
n1 = ds.Tables(0).Rows(intCurrentIndex).Item("NumberOne")
n2 = n1 * n1
ds.Tables(0).Rows(intCurrentIndex).Item("NumberTwo") = n2
Next
' If this is commented out, then da.Update(ds) below gives:
' An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
' Additional Information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
ds.AcceptChanges()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
txtError.Text = "No Exceptions"
Try
conn.Open()
Catch ex As OleDb.OleDbException
txtError.Text = "OleDbException"
GoTo errExit
Catch ex As DataException
txtError.Text = "DataException"
GoTo errExit
Catch ex As Exception
txtError.Text = "Other Exception"
GoTo errExit
End Try
ERRLOC:
' THE PROBLEM IS HERE
' The dataset is not being updated back to the data adapter
' Save dataset to database
da.Update(ds)
ENDERR:
' Fill second dataset from database
txtError.Text = "Error: ds2 Is Empty"
da.Fill(ds2)
'Check if the Table is empty
If ds2.Tables(0).Rows.Count > 0 Then
dSaved = True
txtError.Text = "ds2: No Error"
End If
errExit:
conn.Close()
End Sub
End Class
我在这里想念什么?
当传递带有已修改行的DataRow集合时,更新需要有效的UpdateCommand。
该错误消息告诉您OleDbDataAdapter没有为其定义UpdateUpdate。定义InsertCommand,UpdateCommand和DeleteCommand属性的最常见方法可能是使用OleDbCommandBuilder对象。例如
Dim da As New OleDb.OleDbDataAdapter(strSQL, conn)
Dim cb As New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句