在 visual basic 行中访问数据库删除

access database in visual basic rows delete

我有这段代码可以从按钮中删除行:

    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter

    con.Open()

    da = New OleDbDataAdapter("Select * from orders", con)
    da.Fill(dt)

    dt.Rows(0).BeginEdit()
    dt.Rows(0).Delete()
    dt.Rows(0).EndEdit()

    Dim cb As New OleDbCommandBuilder(da)

    da.Update(dt)

    frmCheckOut.OrdersDataGridView.DataSource = dt.DefaultView

    con.Close()

但它只删除了一行。我怎样才能使这个删除多行? 抱歉,我是 Visual Basic 的新手。

我不太熟悉 BeginEdit() 和 EndEdit(),但如果我要猜测解决方案,我会使用 ForEach 循环。无论您是要删除所有行还是只删除满足特定条件的行,这都是循环遍历数据表所有行的简单方法。

Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter

con.Open()

da = New OleDbDataAdapter("Select * from orders", con)
da.Fill(dt)

For Each row as DataRow in dt.Rows

    ' ** IF (statement if needs to meet certain condition to delete.)

    row.BeginEdit()
    row.Delete()
    row.EndEdit()

    ' ** END IF

Next

Dim cb As New OleDbCommandBuilder(da)

da.Update(dt)

frmCheckOut.OrdersDataGridView.DataSource = dt.DefaultView

con.Close()