从VB 2015更新MS Access数据库

大卫·约翰逊

我正在尝试从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

  1. 在启动时,程序将数据库加载到数据集中。<<,<,>和>>按钮可用于单步执行数据。

  2. “执行”按钮用“ NumberOne”列中条目的平方填充数据集的“ NumberTwo”列。

  3. “保存”按钮尝试通过数据适配器将修改后的数据集保存回数据库。然后将数据库加载到第二个数据集中,以检查第一个数据集是否已正确保存回数据库中。通过逐步浏览数据,可以发现保存失败。

我的代码:

'**********
' 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用vb.net将MS Access行插入数据库

来自分类Dev

将MS Access数据库中的列值填充到VB .Net Combo-Box下拉值中?

来自分类Dev

vb.net中的紧凑型ms访问数据库,而无需数据库副本

来自分类Dev

MS Access数据库错误

来自分类Dev

仅更新Access数据库中的一列值-VB

来自分类Dev

在VB.NET中使用OLEDB通过DataGridView更新Access数据库

来自分类Dev

在MS Access中访问数据库时更新查询

来自分类Dev

VB .net:如何将值放在复选框上(数据库:MS Access)

来自分类Dev

VB.NET数据库不在MS Access和登录错误

来自分类Dev

无法将数据插入数据库ms-access vb

来自分类Dev

MS Access防止数据库锁定

来自分类Dev

MS Access数据库SQL查询

来自分类Dev

如何在vb.net中将单选按钮true或false插入数据库ms.access?

来自分类Dev

VB.NET和Access数据库趋势

来自分类Dev

VB Access数据库防止重复条目错误

来自分类Dev

VB.NET Access数据库255列限制

来自分类Dev

插入到ms Access数据库

来自分类Dev

MS Access数据库是/否更新不正确

来自分类Dev

VB.NET修改与Access数据库的匹配

来自分类Dev

VB.Net更新数据库无法正常工作

来自分类Dev

使用VB.NET更新SQL数据库

来自分类Dev

使用 VB.Net 将数据绑定文本框中的 string.empty 值更新到 MS Access 数据库中

来自分类Dev

Access 数据库 VB - 在数据库中搜索大多数“最近”记录

来自分类Dev

使用 MS ACCESS 数据库在 VB.net 上执行阅读器错误

来自分类Dev

MS Access 数据库监控查询

来自分类Dev

Access VB连接oracle数据库

来自分类Dev

access数据库中更新sql语句出错[VB2010]

来自分类Dev

通过 VB/Visual Studio 插入 Access 数据库 (2017)

来自分类Dev

数据库未从 VB.NET 数据集更新

Related 相关文章

热门标签

归档