SqlDataAdapter无法正常工作

时间:2013-07-22 12:58:31

标签: vb.net sqldataadapter

我正在尝试使用SqlDataAdapter更新数据库,但我的代码似乎不起作用:

    Dim sitedb As String = BookingApp.Globals.siteDB
    Dim conn As New SqlConnection(sitedb)
    Dim sql As String = "select * from name where BOOKING_REF = 'H2124'"

    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim cb As SqlCommandBuilder
    Dim dt As New DataTable
    Dim c As Integer

    cmd = New SqlCommand(sql, conn)
    da = New SqlDataAdapter(cmd)
    cb = New SqlCommandBuilder(da)
    da.Fill(dt)

    For c = 0 To dt.Rows.Count - 1

        dt.Rows(c)!OVLREF3 = c
        dt.AcceptChanges()
    Next

    da.Update(dt)

据我所知,代码似乎没问题,有人有任何建议吗?

由于

1 个答案:

答案 0 :(得分:3)

删除dt.AcceptChanges,它应该有用。

AcceptChanges会将RowState更改为Unchanged,这会导致DataAdapter无效。更新后AcceptChanges本身会隐含地调用DataAdapter