Access数据库的问题 [英] Problem with Access database

查看:60
本文介绍了Access数据库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在使用此代码将数据行添加到Access数据库:



Hello
I'm using this code to add datarow to Access database:

Dim cmm As New OleDb.OleDbCommand
        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Sandman\Desktop\db.mdb"
        con.Open()
        MsgBox("db is now connect")
        Dim i As Integer
        Dim name, lastname As String
        Dim rw As DataRow
 
        rw = DbDataSet.Tables(0).NewRow()
 
        name = TextBox3.Text
        lastname = TextBox4.Text
 
        rw.Item("name") = name
        rw.Item("lastname") = lastname
 
 
        Try
            DbDataSet.Tables(0).Rows.Add(rw)
            i = Table1TableAdapter.Update(DbDataSet)
            MessageBox.Show("Added")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try





这将是工作和datarow将添加,但此代码是不工作(删除):





It will be work and datarow will be add but this code is not work(Remove):

Dim cmm As New OleDb.OleDbCommand
        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Sandman\Desktop\db.mdb"
        con.Open()
        MsgBox("db is now connect")
        Dim i As Integer
        Dim name, lastname As String
        Dim rw As DataRow
 
        rw = DbDataSet.Tables(0).NewRow()
 
        Name = TextBox1.Text
        lastname = TextBox2.Text
 
        rw.Item("name") = Name
        rw.Item("lastname") = lastname
 
 
        Try
            DbDataSet.Table1.Rows.Remove(rw)
            i = Table1TableAdapter.Update(DbDataSet)
            MessageBox.Show("Removed")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try





错误是:

给定的DataRow不在当前的DataRowCollection中。



我该怎么办?



谢谢



The error is:
The given DataRow is not in the current DataRowCollection.

What should i do?

Thanks

推荐答案

错误信息清楚地告诉您问题。您正在尝试删除数据表中不存在的新行,因此它引发了错误。

删除方法 [ ^ ]



样本代码来自上面的链接

Error message tells you the issue clearly. You're trying to delete a new row which doesn't exists in datatable, so it raised the error.
Remove Method[^]

Sample code from above link
' Test to see if the collection contains the value.
If rowCollection.Contains(TextBox1.Text) Then
    Dim foundRow As DataRow = rowCollection.Find(TextBox1.Text)
    rowCollection.Remove(foundRow)
    Console.WriteLine("Row Deleted")
Else
    Console.WriteLine("No such row found.")
End If


这篇关于Access数据库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆