【问题标题】:OleDBException was unhandled what happen?OleDBException 未处理会发生什么?
【发布时间】:2014-04-23 15:11:29
【问题描述】:

我正在做一个待办事项列表程序,错误是当我点击保存时,它会显示错误 这是完整的代码,下面会有弹出错误消息的代码

Public Class frmForm1

Dim inc As Integer = 0
Dim MaxRows As Integer

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Private Sub frmNavigate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\project.accdb;"
    con.Open()
    sql = "SELECT * from todolist"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "todolist")

    con.Close()

    MaxRows = ds.Tables("todolist").Rows.Count

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub
Private Sub NavigateRecords()

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    If inc <> MaxRows - 1 Then
        inc = inc + 1
        NavigateRecords()
    Else
        MsgBox("No More Rows")
    End If

End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    If inc > 0 Then
        inc = inc - 1
        NavigateRecords()
    ElseIf inc = -1 Then
        MsgBox("No Records Yet")
    ElseIf inc = 0 Then
        MsgBox("First Record")
    End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    If inc <> MaxRows - 1 Then
        inc = MaxRows - 1
        NavigateRecords()
    End If

End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    If inc <> 0 Then
        inc = 0
        NavigateRecords()
    End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    ds.Tables("todolist").Rows(inc).Item(1) = txtitem.Text
    ds.Tables("todolist").Rows(inc).Item(2) = txtdescript.Text
    ds.Tables("todolist").Rows(inc).Item(3) = dtptoday.Text
    ds.Tables("todolist").Rows(inc).Item(4) = dtpcomplete.Text


    da.Update(ds, "todolist")

    MsgBox("Data updated!")

End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    btnSave.Enabled = True
    btnadd.Enabled = False
    btnUpdate.Enabled = False
    btnremove.Enabled = False


    txtitem.Clear()
    txtdescript.Clear()


End Sub

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
    btnSave.Enabled = False
    btnadd.Enabled = True
    btnUpdate.Enabled = True
    btnremove.Enabled = True

    inc = 0
    NavigateRecords()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist")
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

Private Sub btnremove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremove.Click
    If MessageBox.Show("Do you really want to Delete this Record?", _
        "Delete", MessageBoxButtons.YesNo, _
        MessageBoxIcon.Warning) = DialogResult.No Then

        MsgBox("Operation Cancelled")
        Exit Sub
    End If

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("todolist").Rows(inc).Delete()
    MaxRows = MaxRows - 1

    inc = 0
    NavigateRecords()
    da.Update(ds, "todolist")

End Sub


Private Sub btnsummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsummary.Click

    Me.Hide()
    summary.Show()
End Sub
End Class

错误在

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist") 'ERROR!!!!!
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

这一行

da.Update(ds, "todolist")

我已经通过添加这个来解决它

Dim cb As New OleDb.OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"

【问题讨论】:

  • 我想知道:\ 当我运行程序时,它显示行 oledbexception 未处理@thetimmer
  • 数据库是否打开?当我在 Access 中编辑过一次查询但未保存时,我遇到了表锁定问题。我看到了你的凭据,意识到你可能已经想检查这个了,只是把它扔在那里。

标签: vb.net unhandled oledbexception


【解决方案1】:

在您的 Sub frmNavigate_Load() 中,您关闭了连接。要解决此问题,您可以 1) 保持连接打开,或 2) 每次您打算执行数据操作时再次打开它(推荐)。

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        'reopen a connection for your da
        con.open()
        'now you are connected, you can build an INSERT command and then save
        cb.GetInsertCommand()
        da.Update(ds, "todolist")
        'close it again
        con.close()
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

【讨论】:

  • {"INSERT INTO 语句中的语法错误。"} 这是给我的@TimG
  • 您一定还没有使用 CommandBuilder 创建您的 INSERT 语句。我添加了一行代码来做到这一点。顺便说一句,您必须添加类似的 UPDATE 和 DELETE 调用(在您的 Subs 中)。
  • 我已经通过在命令生成器下方添加解决了这个问题,感谢您的回答 btw cb.QuotePrefix = "[" cb.QuoteSuffix = "]"
猜你喜欢
  • 2015-12-25
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多